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.
E
if your conditionals are like that (matching a single value), a simple more elegant way would be:
$results = User::where([
'this' => value,
'that' => value,
'this_too' => value,
...
])
->get();
but if you need to OR the clauses then make sure for each orWhere() clause you repeat the must meet conditionals.
$player = Player::where([
'name' => $name,
'team_id' => $team_id
])
->orWhere([
['nickname', $nickname],
['team_id', $team_id]
])
As per my suggestion if you are doing filter or searching
then you should go with :
$results = User::query();
$results->when($request->that, function ($q) use ($request) {
$q->where('that', $request->that);
});
$results->when($request->this, function ($q) use ($request) {
$q->where('this', $request->that);
});
$results->when($request->this_too, function ($q) use ($request) {
$q->where('this_too', $request->that);
});
$results->get();
use whereIn
condition and pass the array
$array = [1008,1009,1010];
User::whereIn('users.id', $array)->get();
We use this instruction to obtain users according to two conditions, type of user classification and user name.
Here we use two conditions for filtering as you type in addition to fetching user information from the profiles table to reduce the number of queries.
$users = $this->user->where([
['name','LIKE','%'.$request->name.'%'],
['trainers_id','=',$request->trainers_id]
])->with('profiles')->paginate(10);
Use This
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Using pure Eloquent, implement it like so. This code returns all logged in users whose accounts are active.
$users = \App\User::where('status', 'active')->where('logged_in', true)->get();