Laravel Relationship complex with passing dynamic value

45 Views Asked by At

I have a complex query such as

$drivers = Drivers::where('somecondition', $request->condition)
    ->with([
        'license' => function($query) {
            $query->where('is_active', 1);
        },
        'license.licenseReport' => function($query) use ($driver_id) {
            $query->where('driver_id', $driver_id)
                ->where('is_active', 1)
                ->orderBy('created_at', 'DESC');
        },    
    ])
    ->get();

My question is normally i would have $driver_id value set ahead of time but in this scenario i want to use the driver_id as the id from the query itself where the driver_id is the id from the drivers table i am running the query against.

Is this something that is possible where the driver_id being passed into the query is the actual ID from the main drivers table?

0

There are 0 best solutions below