How to Use Has Many Through Relationship in Laravel 10?
Teams applying “How to Use Has Many Through Relationship in Laravel 10?” in a production project can use the documentation on this page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi dev,
You will learn a lot about Laravel 10 from this comprehensive book by using relationship examples. Laravel 10 has several features, which I clearly explained step-by-step using a pivot table. I'll present a straightforward example and provide a solution if you have a query about has many through relationships in Laravel 10. You have come to the right place if you want to view an example of hasmanyusing Laravel 10 inverted. Build a simple Laravel 10 Hasmany via Relation example here.
So in this tutorial, you can understand how to create has many through relationships with migration with a foreign key schema for one to many relationships, create records, attach records, get all records, where condition and everything related to has many through relationship.
In this example, I will create "users", "posts" and "countries" tables. each table is connected with each other. now we will create many to many relationship with each other by using the laravel Eloquent Model. We will first create database migration, then models, retrieve records and then how to create records too. So you can also see the database table structure on the below screen.
Has Many Through Relationship will use "hasManyThrough()" for relation.
Create Migrations:
Now we have to create migration of "users", "posts" and "countries" table. we will also add foreign key with users and posts table. so let's create like as below:
users table migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->foreignId('country_id')->constrained('countries');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
posts table migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->foreignId('user_id')->constrained('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
countries table migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('countries');
}
};
Create Models:
Here, we will create Country model. we will also use "hasManyThrough()" for relationship of both model.
Country Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
use HasFactory;
/**
* Write code on Method
*
* @return response()
*/
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... */
);
}
}
Retrieve Records:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Country;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$country = Country::find(1);
dd($country->posts);
}
}
I hope you understand of one to one relationship...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial