Laravel 8 Eloqunet: How to set OR condition to where clause in Laravel

Viewed 3672

I want to show all the Orders from orders table where the status field of that table, equals to canceled OR ignored.

Currently my query goes like this:

$canceled = Order::where('status', 'canceled')
                  ->where('user_id', $uid)
                   ->latest()
                   ->paginate(2);

So how can I add ignored as well to the where clause here?

2 Answers

I think whereIn will do the trick

$canceled = Order::whereIn('status', ['canceled','ignored'])
                   ->where('user_id', $uid)
                   ->latest()
                   ->paginate(2);

Try this query -

$canceled = Order::where(function($q) {
                   $q->where('status', 'canceled')
                      ->orWhere('status', 'ignored');
                    })
                    ->where('user_id', $uid)
                    ->latest()
                    ->paginate(2);
Related