Laravel Eloquent orWhere() Condition Example

Laravel Eloquent orWhere() Condition Example

Laravel orWhere conditions with eloquent query example. Here, you will learn how to use laravel orWhere eloquent method with query builder and model. And as well as how to use laravel multiple orWhere conditions with queries.

This tutorial will take several examples of laravel orWhere conditions with query builder and eloquent model.

The following syntax represents the laravel orWhere clause:

orWhere(Coulumn_name, Value);

Eloquent orWhere() Condition Example

Now, you can see the following examples of laravel orWhere query with single and multiple conditions:

  • Example 1: Laravel orWhere with Query Builder
  • Example 2: Laravel orWhere with Eloquent Model
  • Example 3: laravel orwhere multiple conditions

Example 1: Laravel orWhere with Query Builder

public function index()
{
    $users = DB::table('users')
                    ->where('id', 1)
                    ->orWhere('email', '[email protected]')
                    ->get();
  
    dd($users);                    
}

Example 2: Laravel orWhere with Eloquent Model

public function index()
{
    $users = User::where('id', 1)
                    ->orWhere('email', '[email protected]')
                    ->get();
  
    dd($users);                    
}

When you dump the above given orWhere queries you will get the following SQL query:

SELECT * FROM users WHERE id = '1' OR email = '[email protected]'

Example 3: laravel orwhere multiple conditions

public function index()
{
$users = User::where('name', 'like' , '%'.$qry.'%')
   ->orWhere(function($query) use($qry) {
        $query->where('email','like','%'.$qry.'%')
              ->where('status','!=',$qry) 
   })->get();
   dd($users);
}

Conclusion

That’s all, you have learned how to use laravel orWhere eloquent method with query builder and model for multiple columns and condtions.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *