Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dk4210's avatar

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.

0 likes
10 replies
sr57's avatar

Just use where (it's and where by default)

dk4210's avatar

Well when I use it for everything, it doesn't return anything.

Like this

$search_term = $request->get('query');
        $Clients = SourceClient::Where('application','=', 2)
        ->Where('name','LIKE','%'.$search_term.'%')
        ->Where('c_id','LIKE','%'.$search_term.'%')
        ->Where('oid','LIKE','%'.$search_term.'%')
        ->get();
        return response()->json($Clients);
		

dk4210's avatar

I also tried this with no success


	$search_term = $request->get('query');
        $Clients = SourceClient::Where('name','LIKE','%'.$search_term.'%')
        ->orWhere('c_id','LIKE','%'.$search_term.'%')
        ->orWhere('oid','LIKE','%'.$search_term.'%')
	->AND('application', '=', 2)
        ->get();
        return response()->json($Clients);

sr57's avatar

It's another pb, just put one where, debug, then put the second one, debug, .... and it'll work.

dk4210's avatar

Strange thing is that if I add the following, it works great

$search_term = $request->get('query');
$Clients = SourceClient::Where('application', '=',  $application)
->Where('name','LIKE','%'.$search_term.'%')   

BUT

if I try to add another where like this. It doesn't return anything

$search_term = $request->get('query');
$Clients = SourceClient::Where('application', '=',  $application)
->Where('name','LIKE','%'.$search_term.'%')  
->Where('oid','LIKE','%'.$search_term.'%')

I tried to shuffle them around like this. This works also

$search_term = $request->get('query');
$Clients = SourceClient::Where('application', '=',  $application)
->Where('oid','LIKE','%'.$search_term.'%')

when I add more than two "Where" statments together, i don't get any results. So Strange!

sr57's avatar

when I add more than two "Where" statments together, i don't get any results

It works fine, that's just that you have no record that matches the 2 where!

Look at your sql* using this doc : https://laravel.com/docs/8.x/queries#debugging

*directly in your db

1 like
dk4210's avatar

I see what you're saying. The issue here is that I need something like an "ANDWHERE"

In my case. I need to have the application id match and the name, oid and c_id. They have to match the search term and the id. See what I mean?

dk4210's avatar

I see when I add the "dd()" it shows the query. Thats a great tip.

newbie360's avatar

@dk4210 actually you already have the answer, if you do

$search_term = $request->get('query');
        $sql = SourceClient::Where('application','=', 2)
        ->Where('name','LIKE','%'.$search_term.'%')
        ->Where('c_id','LIKE','%'.$search_term.'%')
        ->Where('oid','LIKE','%'.$search_term.'%')
        ->toSql();
dd($sql);

so

$search_term = $request->get('query');

$Clients = SourceClient::where('application', 2)
            ->where(function ($q) use ($search_term) {
                $q->where('name','LIKE','%'.$search_term.'%')
                  ->orWhere('c_id','LIKE','%'.$search_term.'%')
                  ->orWhere('oid','LIKE','%'.$search_term.'%');
            })
            ->get();

return response()->json($Clients);
dk4210's avatar
dk4210
OP
Best Answer
Level 5

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.