Merge 'with' and 'whereHas' in Laravel 5
The 'macroable' way (Laravel 5.4+)
Add this inside a service provider's boot()
method.
\Illuminate\Database\Eloquent\Builder\Eloquent::macro('withAndWhereHas', function($relation, $constraint){
return $this->whereHas($relation, $constraint)->with([$relation => $constraint]);
});
In terms of performance you can't really optimize anything here (except if you were to move from eloquent relations to joins). With or without whereHas
, two queries will be run. One to select all users another one to load the related models. When you add the whereHas
condition a subquery is added, but it's still two queries.
However, syntactically you could optimize this a bit by adding a query scope to your model (or even a base model if you want to use this more often):
public function scopeWithAndWhereHas($query, $relation, $constraint){
return $query->whereHas($relation, $constraint)
->with([$relation => $constraint]);
}
Usage:
User::withAndWhereHas('submissions', function($query) use ($id){
$query->where('taskid', $id);
})->get();