How to use WHERE with OR AND OR with PHP Laravel Eloquent Query?

Spread the love

Sometimes, we want to use WHERE with OR AND OR with PHP Laravel Eloquent Query.

In this article, we’ll look at how to use WHERE with OR AND OR with PHP Laravel Eloquent Query.

How to use WHERE with OR AND OR with PHP Laravel Eloquent Query?

To use WHERE with OR AND OR with PHP Laravel Eloquent Query, we call Model::where with a callback that has the query we want to run.

For instance, we write

Model::where(function ($query) {
    $query->where('a', '=', 1)->orWhere('b', '=', 1);
})->where(function ($query) {
    $query->where('c', '=', 1)->orWhere('d', '=', 1);
});

to call where with a function that has

$query->where('a', '=', 1)->orWhere('b', '=', 1);

to return values with a equals 1 or b equals 1.

And we call where again with another function that return results that has c equals 1 or d equals 1.

Conclusion

To use WHERE with OR AND OR with PHP Laravel Eloquent Query, we call Model::where with a callback that has the query we want to run.

Leave a Reply

Your email address will not be published.