Difference between whereOr and orWhere in laravel

I have used whereOr

and orWhere

in my code in Laravel and it works but sometimes gives great result

$user_query = User::select( 'users.id', 'users.username','users.first_name','users.last_name', 'users.photo' )
                    ->where('users.status',1)
                    ->where('users.id','!=',$id)
                    ->where('users.username','like','%'.$searchkey.'%')
                    ->orWhere('users.first_name','like','%'.$searchkey.'%')
                    ->orwhere('users.last_name','like','%'.$searchkey.'%')->get();

                    // ->whereOr('users.first_name','like','%'.$searchkey.'%')
                    // ->whereOr('users.last_name','like','%'.$searchkey.'%')->get();

      

What is the difference between whereOr

andorWhere

+3


source to share


1 answer


They can both do the same. But actually, since you are using it whereOr

wrong. whereOr

- dynamic , where. They are described in more detail in this blog.

With dynamic elements, the condition is determined not only by the parameters, but also by the method name itself. Here's an example:

->whereAge(18);

      

it can be translated into

->where('age', '=', 18);

      



This is done with a function __call()

that then calls dynamicWhere()

. This method then decodes the method name you called and stores that information as a where clause.

Now with, whereOr

you really need to call it like this:

->whereAgeOrGender(18, 'male');

      

This means that it gives you a simple syntax, but much less flexible than the "real" ones. For example, it will always use the operator=

. So this is definitely not the right fit for your case.

+2


source







All Articles