Laravel 9 Has Many Through Eloquent Relationship Example

In this example, we will show you how to use the Has Many Through Eloquent Relationship in Laravel 9.

We will create three tables: users, posts, and comments. The users table will contain the user’s information, the posts table will contain the post’s information, and the comments table will contain the comments related to the post.

We will create a model for each table and define the relationships between them.

The User model will have a hasMany relationship with the Post model and the Post model will have a hasMany relationship with the Comment model.

We will also create a HasManyThrough relationship between the User and Comment models. This will allow us to access the comments related to a user’s posts.

Finally, we will create a controller to demonstrate how to use the HasManyThrough relationship.

In the controller, we will create a method to get all the comments related to a user’s posts. We will use the HasManyThrough relationship to get the comments and then return them in a JSON response.

We hope this example has helped you understand how to use the HasManyThrough Eloquent relationship in Laravel 9.
[ad_1]

Laravel 9 HasManyThrough relationship example; In this tutorial, you will learn how to create hasmany through relationship in eloquent models and as well as how to use it.

Laravel Eloquent HasMany Through Relationship Example

Create migration of “users”, “posts” and “countries” table and add a foreign key with users and posts table as follow:

In users migration file:

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');

});

In posts migration file:

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');

});

In countries migration file:

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

    $table->increments('id');

    $table->string('name');

    $table->timestamps();

});

The “has-many-through” relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country model might have many Post models through an intermediate User model. In this example, you could easily gather all blog posts for a given country. Let’s look at how to define HasMany Through relationship:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    /**
     * Get all of the posts for the country.
     */
    public function posts()
    {
        return $this->hasManyThrough('App\Post', 'App\User');
    }
}

To fetch data using this relationship as follow:

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

Recommended Laravel Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00