Multiple orWhere Condition in Laravel Eloquent

By Hardik Savani | July 20, 2020 | 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 and laravel 8 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...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: