Laravel Has Many Through Eloquent Relationship Example

Admin   Laravel   113  2021-03-16 01:50:03

Hi Guys,

In this tutorial, I Will explain how to create laravel Has many Through eloquent relationship.We will show Has many Through model relationship in laravel. For example, a country is connected with users and users with posts, then we can access all posts connected with a specific country.

you can understand how to create Has many through relationships with migration with a foreign key schema for Has many relationships, create records, attach records, get all records, where condition and everything related to has many through relationship.

In this example , we will create "user","post" and "country" table both table are connected with each other, i will create Has many Through relationship with each other by using laravel Eloquent Model, We will first create database migration, then model

Here the example of laravel Has many Through eloquent relationship.

Step:1 Create Migration

In this example, we will need two tables in our database employee and salary.I will start to create the model and migrations, then we will define the many to manmany relationship.To generate models and migrations, run below two commands in your terminal.

 

php artisan make:model User -m

php artisan make:model Post -m

php artisan make:model Country -m

Above command will generate two models in your app folder called Employee and Salary. You will also find two new migrations in your database/migrations folder.

Your newly created models and migration after add table field:

Path:database\migrations\2014_10_12_000000_create_users_table.php

 

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->id();

$table->string('name');

$table->string('email')->unique();

$table->timestamp('email_verified_at')->nullable();

$table->string('password');

$table->integer('country_id');

$table->rememberToken();

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('users');

}

}

Path:database\migrations\2014_10_12_000000_create_posts_table.php

 

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->id();

$table->string("name");

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

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

}

Path:database\migrations\2014_10_12_000000_create_countries_table.php

 

<?php