The Has Many Through connection is a little difficult to grasp, but it provides a quick way to get data from another mode relationship. If, for example, a country is linked to users and users to posts, we can view all posts associated with that country. In Laravel 6, Laravel 7, Laravel 8, and Laravel 9, there are several via relationships.
So, in this lesson, you'll learn how to establish has many through relationships using migration and a foreign key schema for one-to-many connections, as well as how to create records, attach records, get all records, use where conditions, and more.
I'll make tables for "users," "posts," and "countries" in this example. Each table is linked to the others. Using Laravel Eloquent Model, we will now construct many to many relationships with each other. We'll start with database migration, then move on to models, retrieving records, and finally creating records.

"hasManyThrough()" will be used for relation in Has Many Through Relationship.

1. Create Migrations

Now we must migrate the tables "users," "posts," and "countries." With the users and posts tables, we'll also add a foreign key. So, let's make something like this:

users table migration
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->integer('country_id')->unsigned();
    $table->rememberToken();
    $table->timestamps();
    $table->foreign('country_id')->references('id')->on('countries')
                ->onDelete('cascade');
});
 
posts table migration
Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string("name");
    $table->integer('user_id')->unsigned();
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade');
});
countries table migration
Schema::create('countries', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

2. Create Models

We'll make a country model here. For the relationship between the two models, we'll utilise "hasManyThrough()"

Country Model
<?php
 namespace App;
 use Illuminate\Database\Eloquent\Model;
 class Country extends Model
 {
    public function posts()
    {
        return $this->hasManyThrough(
            Post::class,
            User::class,
            'country_id', // Foreign key on users table...
            'user_id', // Foreign key on posts table...
            'id', // Local key on countries table...
            'id' // Local key on users table...
        );
    }
 }
?>

3. Retrieve Records

$country = Country::find(1);    
dd($country->posts);

I hope you will like the content and it will help you to learn Laravel Has Many Through Eloquent Relationship
If you like this content, do share.


Recommended Posts

View All

Install and Use Font Awesome Icons in Laravel 9


laravel 9 install font awesome icons example,how to install font awesome icons in laravel 9,install font awesome icons example,how to install font awe...

How to Install and Use MomentJS in Laravel 9 Application


Learn how to easily install and use MomentJS in your Laravel 9 application with our step-by-step guide. Improve your date and time management today!

Crop Image Before Upload Using Croppie.js in Laravel 9


Using Laravel 9, crop an image before uploading it using Croppie.js. Before uploading an image, Laravel uses Ajax to crop it with croppie. Cropping an...

Laravel Eloquent Query - Laravel Group By with Order By Desc


You&amp;#039;ve come to the right place if you&amp;#039;re looking for an example of a laravel eloquent group by order by desc.

How To Use WYSIHTML5 Editor In Laravel 9?


wysihtml5 editor example, laravel wysihtml5 editor example, wysihtml5 laravel, wysihtml5 editor example in laravel, wysihtml5 with laravel, wysihtml5...