How to use orWhere() clause in Laravel?

Last updated on February 28, 2023

As we know that Laravel supports the MySQLi database. In this database, we are allowed to run queries with multiple or-where clauses. Laravel included eloquent that provides the orWhere() method that adds or where clauses into the query. Eloquent method orWhere() accepts three parameters in which the first is required and the other two are optional. It also accepts closure, which helps when multiple clauses are needed.

Build Query Using orWhere() Clauses

Let’s build different queries to fetch records from users table using orWhere() method.

Empty orWhere() Clause

In the below query, we have added an empty orWhere() Clause.

$users = User::orWhere()->get()->toArray();

This query wouldn’t return any result, instead, it would return an error that would be something like below

Too few arguments to function Illuminate\Database\Eloquent\Builder::orWhere(), 0 passed in

This error means orWhere() method can’t run without having an argument.

Add orWhere() Clause With Column Name Only

Eloquent method orWhere() is smart enough, it would add is null condition when it finds column name only.  The below query is an example,

$users = User::orWhere('name')->get()->toArray();

This query wouldn’t return any result or error but an empty array. If we convert it into a raw query by using Laravel’s toSql() method, it would be something like below

select * from `users` where `name` is null

The eloquent method toSql() creates a raw query string from the query builder in Laravel, which beyondco.de has explained well.

Add orWhere() Clause With Key Value

This is a normal way to use orWhere() method. In this case, we pass the first argument as the column name and another its value. Following is an example,

$users = User::orWhere('name', 'john')->get()->toArray();

The above query would find ‘john’ name in the name column then fetch the rows and return it into array format. For a single row, we should use first() instead of get() method. Below is the raw version of the above query,

select * from `users` where `name` = ?

Add orWhere() Clause With Closure

In some cases, we use multiple where clauses. In this case, orWhere() method accepts closure which helps to add multiple where clauses. Below is the example,

$users = User::orWhere(function($query) {
    $query->where('name', 'john')
            ->where('email', '=', 'john@gmail.com');
})->get()->toArray();

Following is the raw version of the above query

select * from `users` where (`name` = ? and `email` = ?)

Conclusion:

In this article, we have covered the following,

  • Eloquent method orWhere() clause introduction
  • What happened when the empty orWhere() clause added
  • Add orWhere() clause with key and value
  • Add orWhere() clause with closure


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,