Используйте , когда , чтобы применить оператор $q->where('id', $id)->where('log', 'like', '%' . $log . '%')
, если присутствует заданное значение $id
:
$postlocations = $postlocationsquerynew
->whereHas('postlog', function (Builder $querypost) use($request){
$log = $request->log;
$id = $request->id;
$querypost->when($id, function($q, $id) use ($log) {
$q->where('id', $id)->where('log', 'like', '%' . $log . '%');
});
})
->get();