Today, I will show how to use Has Many Through relationship in laravel 12 application in this blog. We will use hasManyThrough() method for has many through relationship.
In this application, a “hasManyThrough” relationship helps to access distant relationships where we have three tables: “users,” “posts,” and “countries.” A user can have many posts, which belongs to each country. By using “Has Many Through,” we directly can access posts from a country. It simplifies queries, allowing for seamless navigation through multiple related tables.
hasManyThrough Relationship will use “hasManyThrough()” for relation.
Create Migrations
Create migration of “users“, “posts” and “countries” table by adding foreign key with users and posts table. So 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'); } };
Read More Blog Like That
How to Use Laravel 12 and Bootstrap 5 together An Easy Guide
How to Use Laravel 12 and Bootstrap 5 together An Easy Guide
Laravel 12 Tinker with Example
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
We will create Country model by using “hasManyThrough()” for relationship of both model.
Country Model
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasManyThrough; class Country extends Model { use HasFactory; /** * Write code on Method * * @return response() */ public function posts(): HasManyThrough { 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);
}
}