Laravel eloquent whereHas using value from other relation

Viewed 27

Let's say destinationCity has a destCityCode value and dispatchDepot has a depotCityCode value.

Order has a hasOne() relationship to a dispatchDepot model and a hasMany() relationship with destinationCity

Order::with('destinationCity', 'dispatchDepot')
  ->whereHas('destinationCity', function($q) {
    // is there a way to use values from the dispatchDepot relation in this callback
    // i.e. something like this...?

    $q->where('destCityCode', '=', 'depotCityCode');
  })
  ->get();

Is this something where I have to resort to using a join, or is there some way to achieve what I've outlined above using Eloquent?

Many thanks,

Jerry

0 Answers
Related