How to apply Mysql AND/OR clause in Laravel


Problem :

I am trying to make a MySQL query inside Laravel.

> "select * from users u where (name like
> '%".$request->search_string."%' or email like
> '%".$request->search_string."%') and (user_type=2)";

I tried the code below

public function searchUsers(Request $request){
        $query = DB::table('users as u');
        $query->where('u.user_type',2);
        $query->where(function($query,$request){
            $query->orwhere('u.name','LIKE','%'.$request->search_string.'%');
            $query->orwhere('u.email','LIKE','%'.$request->search_string.'%');
        });
        $result['all_users'] = $query->get();
    return Response::json($result);
}

But I am getting the following error

Missing argument 2 for App\Http\Controllers\PatientController::App\Http\Controllers{closure}()



Solution :

You have syntax error in where clause. See this:

public function searchUsers(Request $request){
        $query = DB::table('users as u');
        $query->where('u.user_type',2);
        $query->where(function($query)use($request){ // Here  is the change
                    //  ^^ Pass only one parameter to closure function and pass `$request` in `use` function
            $query->orwhere('u.name','LIKE','%'.$request->search_string.'%');
            $query->orwhere('u.email','LIKE','%'.$request->search_string.'%');
        });
        $result['all_users'] = $query->get();
    return Response::json($result);
}

Mysql Tutorials

Mysql Howto..

How to use a shorthand for calling in fields in mysql query

How to join a many to many relationship

How to change name of table while creating it through Stored procedure

How to start mysql from Linux

How to order by a count(*) field?

How can I query between two dates with the 'date' field is a string?

How to Parse PHP Searilize data in Mysql Stored Procedure & Looping it

How to select two or more columns of a table and send through GET / POST?

How do I find the difference between today's date and X days from today?

how to echo table in php by mysql

How can I access the table comment from a mysql table?

How to cast datetime of mysql to datetime of asp.net

How to select specified rows

Mysql show data in Pivot View

How to get MySQL value from certain row?

How do I select 10 random rows from a MYSQL Database, with unique values in column x?

How to return exact matches in mySQL query using WHERE IN [closed]

How to do an arithmetic operation in MySQL

how to identify the source table of fields from a mysql query

How to compare 2 lists and merge them in Python/MySQL?

How should I store types of coupons in my db?

How to return all data with duplicate columns using table aliases

How to make a valid request to mysql with array of tables?

How to Create a MySql Database using JDBC

How to compare 2 mysql table with a primary key in [closed]

How to count and limit record in a single query in MYSQL?

MySQL how to timestamp only one column update in same table

How to add suctom column based on some condition in query

How to implement “WHERE NOT IN” query into Codeigniter Active records?

MySQL Notice: Undefined index: How to resolve [duplicate]