I try to achieve that in a scope (would have been smart to share that detail).
//Allocation.php
...
public function scopeWithMatchingDrivers($query)
{
return $query->whereHas('vehicle', function($driverQuery){
$driverQuery->where('driver_id', '<>', DRIVER_ID_OF_ALLOCATION);
});
}
Passing a value into the closure would be great but there is not the single $driver Object I want to test against, but I want to compare the driver_id of each Allocation with the driver_id of the related vehicle.
Of course I could retrieve data and then loop through it but I would be nice to solve this just by using the query builder.
I hope I was able to clarify better what I pursue.