Laravel has one through relationship tutorial

In these tutorials, we can retrieve the relevant record of a particular model using a simple query. But sometimes, we have a situation where two models relate using the third model. In Laravel, chic relationships are defined in model classes. Laravel eloquent offers easy ways to build common relationships

Let's take an example. The passenger model has a vehicle model and the vehicle model has a driver model and you want to get a driver for a specific passenger model. In this respect, there is no direct relationship between the passenger model and the driver model. They are connected by a vehicle model.

Step 1 : Create Migrations

Now we have to create migration of "passenger", "vehicle" and "drivers" table. In this relationship, we have three database tables

php artisan make:migration create_passengers_table
php artisan make:migration create_vehicles_table
php artisan make:migration create_drivers_table

passengers migration

public function up()
{
    Schema::create('passengers', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });
}

vehicles migration

public function up()
{
    Schema::create('vehicles', function (Blueprint $table) {
        $table->id();
        $table->integer('passenger_id');
        $table->string('number');
        $table->timestamps();
    });
}

drivers migration

public function up()
{
    Schema::create('drivers', function (Blueprint $table) {
        $table->id();
        $table->integer('vehicle_id');
        $table->string('name');
        $table->timestamps();
    });
}

Run the migrate command to create tables into database.

php artisan migrate

Step 2 : Create Model

NOw we need 3 table so create one by one

Passenger Table

php artisan make:model Passenger
php artisan make:model Vehicle
php artisan make:model Driver

We have the passenger_id field in the table of vehicles and the vehicle_id in the driver field. There is no direct relationship between the table of passengers and drivers. To define the relationship between the passenger and the driver table, create a method in the passenger model.

/**
 * Get the driver of vehicle.
 */
public function vehicleDriver()
{
    return $this->hasOneThrough(Driver::class, Vehicle::class);
}

Pash other peramiter

/**
 * Get the driver of vehicle.
 */
public function vehicleDriver()
{
    return $this->hasOneThrough(
        Driver::class,
        Vehicle::class,
        'passenger_id', // Foreign key on the vehicles table
        'vehicle_id', // Foreign key on the drivers table
        'id', // Local key on the vehicles table
        'id' // Local key on the drivers table
    );
}

Step 3 : Get Records:

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $driver = Passenger::find(1)->vehicleDriver;

    dd($driver);
}

I hope this will help you to understand has one through eloquent relationship in laravel app.

All Laravel relationship TUtorials

Laravel has one through relationship tutorial