Laravel 8 Has Many Through Relationship Example

Websolutionstuff | Nov-17-2021 | Categories : Laravel PHP MySQL

In this example we will see laravel 8 has many through relationship example. hasManyThrough relationship difficult to understand compare to other relationship. you use hasManyThrough relationship laravel 6, laravel 7 and laravel 8. The has-many-through relationship provides a convenient way to access distant relations via an intermediate relation. 

For example, a categories is connected with products and products connected with orders, then we can access all orders connected with a specific categories. So, simply you can access or get data using intermediate model relation using hasManyThrough in laravel 8.

Now, we will create categories, products and orders table. categories table connected with products and products table connected with orders table like below screenshot and we also create migration and model for all table and retrive data using model.

laravel_has_many_through_relationship

 

 

Create Migration:

Categories Table :

Schema::create('categories', function (Blueprint $table) {

    $table->increments('id');

    $table->string('name');

    $table->timestamps();

});

Products Table :

Schema::create('products', function (Blueprint $table) {

    $table->increments('id');

    $table->integer('categories_id')->unsigned();

    $table->timestamps();

    $table->foreign('categories_id')->references('id')->on('categories')->onDelete('cascade');

});

Orders Table : 

Schema::create('orders', function (Blueprint $table) {

    $table->increments('id');

    $table->integer('product_id')->unsigned();

    $table->timestamps();

    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

});

 

Create Model :

Now, we will create categories model and define relationship on model.

Category Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{    
    public function order()
    {
        return $this->hasManyThrough(Order::class, Product::class);
    }
}

 

 

The first argument passed to the hasManyThrough method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.

Though the Order model table does not contain category_id column, the hasManyThrough relation we can access to $categories->orders like this.

 

Retrive Records

Now, retrive record using intermediate model through like below code example.

$category = Category::find(1);	
 
dd($category->order);

 


You might also like :

Recommended Post
Featured Post
How To Create Dynamic Bar Chart In Laravel
How To Create Dynamic Bar Char...

In this article, we will show you how to create a dynamic bar chart in laravel. charts are used to represent data i...

Read More

Jul-01-2020

Laravel 9 Socialite Login With Twitter Account
Laravel 9 Socialite Login With...

In this article, we will see laravel 9 socialite login with twitter account. Many websites provide different t...

Read More

Nov-12-2022

How To Create Multi Language Website In Laravel 9
How To Create Multi Language W...

In this article, we will see how to create a multi language website in laravel 9. In this example, we will see the...

Read More

Apr-20-2022

How To Import Export Excel & CSV File In Laravel 10
How To Import Export Excel & C...

In this article, we will see how to import and export Excel & CSV files in laravel 10. Here, we will learn about lar...

Read More

Mar-08-2023