I want to write below queries but it doesn't produce the result that I want.
SELECT * FROM TABLE1 WHERE COLUMN1 = 'DATA' AND ( COLUMN2 LIKE 'K%' OR COLUMN3 LIKE 'K%' OR COLUMN4 = '[email protected]')
I want above queries eloquent in Laravel 9, I have written as below but still doesn't produce the same result.
DB::table('table1')->where(function ($query) use ($request) {
$query->where('column2', 'LIKE', $request -> data)
-> orWhere('column3', 'LIKE', $request -> data )
-> orWhere('column4', '=', $request -> data ) ;
})->get();
Thanks in advance.
CodePudding user response:
You are missing the first where
about COLUMN1
isn't?
Currently, it produces:
SELECT * FROM table1 WHERE (column2 LIKE 'a' OR column3 LIKE 'b' OR column4 = 'c')
Update your code to include the first where
:
DB::table('table1')
->where('column1', 'DATA')
->where(function ($query) use ($request) {
$query->where('column2', 'LIKE', $request->data)
->orWhere('column3', 'LIKE', $request->data )
->orWhere('column4', '=', $request->data ) ;
}
)->get();
And now, it should work as expected (at least, it will produce the query you are expecting).