I have a query:
Posts::whereHas(\'comments\', function($query){
$query->where(\'name\', \'like\', \'asd\');
})->with(\'comments\')->get
You can create query something like this, almost without duplicate :)
$callback = function($query) {
$query->where('name', 'like', 'asd');
}
Posts::whereHas('comments', $callback)->with(['comments' => $callback])->get();
Or, you can make like in this post Laravel - is there a way to combine whereHas and with