The problem is that you have code like this:
WHERE foreignKey = X AND first_name = Y OR last_name = Z
Thus, it is obvious that he is not returning what he needs.
Now, to make it work, you need to add these restrictions as a subdirectory, where:
$bookings=$bookings->whereHas('client',function($q) use ($client_name){ $q->where( function ($q) use ($client_name) { $q->where('first_name','LIKE',$client_name) ->orWhere('last_name','LIKE',$client_name); }); });
This will result in a query like:
WHERE foreignKey = X AND (first_name = Y OR last_name = Z)
what you need.
Jarek tkaczyk
source share