Laravel 9 Has Many Through Relationship Example

Websolutionstuff | Apr-04-2022 | Categories : Laravel PHP

In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is difficult to understand compared to other relationships. You can 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.

So, let's see has many through in laravel 9, has many through pivot laravel 9, hasmanythrough laravel 9 example, laravel 9 has many through.

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 9.

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 retrieve data using model.

laravel_9_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
Character Count In Textarea
Character Count In Textarea

In this article, we will explain to you how to count characters from textarea. many times a client has requirements...

Read More

Jul-08-2020

Laravel 8 One To Many Polymorphic Relationship
Laravel 8 One To Many Polymorp...

In this tutorial we will learn about laravel 8 one to many polymorphic relationship. A one-to-many polymorphic rela...

Read More

Nov-19-2021

How to Run Specific Seeder in Laravel 8
How to Run Specific Seeder in...

In this example, we will learn how to run a specific seeder in laravel 8. If you want to run only one seeder in laravel...

Read More

Jan-19-2022

How To Reset Modal Form In jQuery
How To Reset Modal Form In jQu...

Have you ever seen those pop-up boxes on websites? They're called modal forms, and they make it easier to do things...

Read More

Jan-04-2023