I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE
clause on multiple conditions. It works, but it's not elegant.
Example:
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Is there a better way to do this, or should I stick with this method?
You can use subqueries in anonymous function like this:
Without a real example, it is difficult to make a recommendation. However, I've never needed to use that many WHERE clauses in a query and it may indicate a problem with the structure of your data.
It may be helpful for you to learn about data normalization: http://en.wikipedia.org/wiki/Third_normal_form
use
whereIn
condition and pass the array$array = [1008,1009,1010];
User::whereIn('users.id', $array)->get();
In this case you could use something like this:
It should supply you with a query like:
You can use array in where clause as shown in below.