Back

Laravel developers, friendly reminder: always put your orWhere queries carefully 👀

Always try to put them in a separate where 👌

2

195

This will work correctly but if you want to add 3rd where which is not dependent with active status if it fails it still takes that entry as it has status active.

$users = User::query()
->where('role', 'admin')
->orWhere('status', 'active')
->get();

1

219

So if you add something like this..

$users = User::query()
->where('is_visible', true)
->where('role', 'admin')
->orWhere('status', 'active')
->get();

It will take hidden posts too if they have the active status 😬

1

175

In response to @MrPunyapal

The correct solution will be...

$users = User::query()
->where('is_visible', true)
->where(function (Builder $query) {
$query->where('role', 'admin')
->orWhere('status', 'active');
})
->get();

1

239

  • No matching results...
  • Searching...

/ 1000

You can also use whereAny for Laravel 10.47+

108