Just use where (it's and where by default)
Laravel query with 'LIKE'
Hello Guys,
So I've created a live search and have the following rows in my table id, application, name, c_id & o_id.
I'm having an issue where I need it to search the table and bring back only the matches from the application value, but instead it's searching everything due to the "Orwhere" statements. I can't find an "AndWHERE".
Here's my current query.
$search_term = $request->get('query');
$Clients = SourceClient::Where('application','=', 2)
->orWhere('name','LIKE','%'.$search_term.'%')
->orWhere('c_id','LIKE','%'.$search_term.'%')
->orWhere('oid','LIKE','%'.$search_term.'%')
->get();
return response()->json($Clients);
I basically need it to only search the search terms based on id 2. This will ofcourse be dynamic, but for now I just want it to search based on application id of "2".
Thanks in advance.
Thanks for you help. This is what worked. I used parameter grouping
$application = 2;
$search_term = $request->get('query');
$Clients = SourceClient::Where('application',$application)
->where(function($query) use ($search_term)
{
$query->Where('name','LIKE','%'.$search_term.'%');
$query->OrWhere('oid','LIKE','%'.$search_term.'%');
$query->OrWhere('c_id','LIKE','%'.$search_term.'%');
})
Please or to participate in this conversation.