0

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?

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
Yeak
  • 2,470
  • 9
  • 45
  • 71
  • Note however that `Builder::with()` performs 2 separate queries. Relationships are not retrieved with joins, so if you're trying to compare something on the original model with something on the related model that won't work. – miken32 Jul 11 '23 at 22:04
  • Thank you @miken32 it did work. I feel dumb for not realizing i can do this especially when i write the same for join statements. Thank you – Yeak Jul 11 '23 at 22:38
  • @hakre Laravel is a framework built in PHP, no need to strip [tag:php] from the question – miken32 Jul 12 '23 at 16:01
  • 1
    @miken32 Related: https://meta.stackoverflow.com/a/425533/3965631 – Tim Lewis Jul 13 '23 at 22:41

0 Answers0