Multiple orWhere Condition in Laravel Eloquent

By Hardik Savani April 16, 2024 Category : Laravel

This post will give you example of multiple orwhere condition in laravel. I’m going to show you about laravel eloquent multiple orwhere. i would like to share with you laravel multi orwhere. We will look at example of how to write multiple orwhere in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

If you need to use sql multiple or where query in laravel then you can use it. Laravel provide orWhere() to use sql or query query multiple time. in or where() we just need to pass two argument one is column name and will be value.

You can see bellow syntax on orWhere query in laravel:

orWhere(Coulumn_name, Value);

Now i will give you two example of how to use multiple orWhere query in laravel application. So let's see those example how it works.

Example 1:

SQL Query:

"select * from `users` where `first_name` LIKE ? or `last_name` LIKE ? or `email` LIKE ?"

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$search = "Har";

$users = User::select("*")

->where('first_name', 'LIKE', '%'.$search.'%')

->orWhere('last_name', 'LIKE', '%'.$search.'%')

->orWhere('email', 'LIKE', '%'.$search.'%')

->get();

dd($users);

}

}

Example 2: Multiple orWhere with where condition

SQL Query:

"select * from `users` where `status` = ? and (`first_name` LIKE ? or `last_name` LIKE ? or `email` LIKE ?)"

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$search = "Har";

$users = User::select("*")

->where('status', 1)

->where(function($query) use ($search){

$query->where('first_name', 'LIKE', '%'.$search.'%')

->orWhere('last_name', 'LIKE', '%'.$search.'%')

->orWhere('email', 'LIKE', '%'.$search.'%');

})

->get();

dd($users);

}

}

I hope it can help you...

Shares