Laravel SQL Query

Home / Uncategorized / Laravel SQL Query

Question:
I have sample database
id | Name | Email | Group
1 John Doe jdemo@yahoo.com A
2 Robert Chow robertdemo@gmail.com B
3 Lee Tee leedemo@aol.com C

I use autocomplete(Ajax for search and to show data on table)

On my SearchController

This is my Code
if ($request->ajax()) {
$output = "";
$orderinfo=DB::table(‘tb_client’)
->where(’email’,’LIKE’,’%’ .$request->search. ‘%’ )
->orWhere(‘id’,’LIKE’,’%’ .$request->search. ‘%’)
->orWhere(‘group’,’LIKE’,’%’ .$request->search. ‘%’)
->get();
}

I want to have filter this that Only Group (A) should be listed on my html table

so if user search for Lee Tee it will show not belong to your Area ,


Answer:
Change your where clause :$orderinfo = DB::table(‘tb_client’)
->where(function ($query) {
$query->where(’email’,’LIKE’,’%’ .$request->search. ‘%’)
->orWhere(‘id’,’LIKE’,’%’ .$request->search. ‘%’);
})
->where(‘group’, ‘A’)
->get();

Also I don’t recommend to use LIKE on id field!
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *