I have this code in Laravel 5, using Eloquent, which is working perfectly:
$filterTask = function($query) use ($id) { $query->where(\'taskid\', $id);
The 'macroable' way (Laravel 5.4+)
Add this inside a service provider's boot() method.
boot()
\Illuminate\Database\Eloquent\Builder\Eloquent::macro('withAndWhereHas', function($relation, $constraint){ return $this->whereHas($relation, $constraint)->with([$relation => $constraint]); });