Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent grouped orWhere

Currently have an Eloquent statement:

$contacts = Contacts::where('lname','LIKE',$searchquery.'%')
    ->orWhere('fname','LIKE',$searchquery.'%')
    ->orWhere('phone','LIKE','%'.$searchquery)
    ->where('active','=',1)->get();

It is treating it as

select 
    * 
from 
    contacts 
where 
    lname like $searchquery+'%' 
    or lname like $searchquery+'%' 
    or lname like $searchquery+'%' 
    and active = 1

what I am needing is

select 
    * 
from 
    contacts 
where 
    (lname like $searchquery+'%' 
    or lname like $searchquery+'%' 
    or lname like $searchquery+'%') 
    and active = 1

How do I go about grouping in Eloquent? I have found a couple examples such as:

DB::table('users')
        ->where('name', '=', 'John')
        ->orWhere(function ($query) {
            $query->where('votes', '>', 100)
                  ->where('title', '<>', 'Admin');
        })
        ->get();

But I am only used to Eloquent, not Laravels DB Query builder. I tried adapting the Eloquent form to this

$contacts = Contacts::->where('active', '=', '1')
        ->where(function ($query) {
            $query->orWhere('lname', 'LIKE', $searchquery.'%')
                  ->orWhere('lname', 'LIKE', $searchquery.'%')
                  ->orWhere('phone', 'LIKE', '%'.$searchquery);
        })
        ->get();

No success as it does not recognize the $searchquery inside the function.

What am I missing?

like image 553
Keith Clark Avatar asked Dec 09 '15 23:12

Keith Clark


People also ask

How do you use orWhere in Laravel eloquent?

you can simple use orWhere eloquent method in laravel 6, laravel 7, laravel 8 and laravel 9. If you need to use sql or where query in laravel then you can use it. Laravel provide orWhere() to use sql or query query. in or where() we just need to pass two argument one is column name and will be value.

Is eloquent an ORM?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

What is eloquent and query builder in Laravel?

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses. Active Record is a good solution for processing a single entity in CRUD manner - that is, create a new entity with filled properties and then save it to a database, load a record from a database, or delete.

What is advantage of eloquent in Laravel?

Faster Access To The Database: Eloquent ORM is Laravel's built-in ORM implementation feature. This object-relational mapper is used to interact with relational databases. As it is a complete package on its own, so a developer can use it inside Laravel or outside Laravel.


2 Answers

So, this is what you have to do:

DB::table('users')->where(function($query) use ($searchQuery){
                                $query->where('lname', 'LIKE', $searchQuery . '%')
                                      ->orWhere('fname', 'LIKE', $searchQuery . '%')
                                      ->orWhere('phone','LIKE', '%' . $searchquery);
                          })
                          ->get();

Note that I've put use ($searchQuery) so it can be used inside the closure

like image 104
Eduardo Pacios Avatar answered Sep 22 '22 15:09

Eduardo Pacios


When you use orWhere with multiple where clauses, you need to be careful!

Where clauses after orWhere doesn't effected to sibling where clauses before orWhere clauses such as where, whereHas, whereDoesntHave, orWhereHas

also you have to pass $searchquery variable inside to function in where clause using use ($searchquery)

Contacts::->where(function ($query) use ($searchquery) {
                $query->orWhere('lname', 'LIKE', $searchquery.'%')
                      ->orWhere('lname', 'LIKE', $searchquery.'%')
                      ->orWhere('phone', 'LIKE', '%'.$searchquery);
            })
            ->where('active', '=', '1')
            ->get();
like image 26
Chanuka Asanka Avatar answered Sep 22 '22 15:09

Chanuka Asanka