Multiple orWhere Condition in Laravel Eloquent

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

This post will give you lesson 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 lesson 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 lesson of how to use multiple orWhere query in laravel application. So let's see those lesson how it works.

Lesson 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);

}

}

Lesson 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);

}

}

thank you very much for following up with me.

Recent Posts

Related Posts