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

vandan's avatar
Level 13

whereHas filter query issue

hello guys i want result like i have many products and in products have many supplier and one customer so when i search product description than display all product but i want output like product display but match with customer id

here is my code

	$pi = Product::where('customer_id',$customerId)
        	->whereHas('supplier', function (Builder $query) use($search) {
            		$query->where('sys_state', '!=', '-1')
            		->orWhere('name','LIKE','%' . $search . '%');
        	})
        	->where('sys_state','!=','-1')
        	->orWhere('prd_our_item_no', 'LIKE', '%' . $search . '%')
        	->orWhere('prd_supplier_item', 'LIKE', '%' . $search . '%')
        	->orWhere('prd_description','LIKE','%' . $search . '%')
        	->get();

i try this query but all the product display when i search customer id where condition not work

0 likes
22 replies
vandan's avatar
Level 13

@webrobert try this but search not working still same issue nothingcan search

  $pi = Product::where('customer_id', $customerId)
            ->where('sys_state', '!=', '-1')
            ->whereHas('supplier', function (Builder $query) use ($search) {
                $query->where('sys_state', '!=', '-1')
                ->orWhere(function ($query) use ($search) {
                    $query->where('name', 'LIKE', '%' . $search . '%')
                    ->where('sys_state','!=','-1');
                });
            })
            ->orWhere(function ($query) use ($search, $customerId) {
                $query->where('prd_description', 'LIKE', '%' . $search . '%')
                ->where('sys_state', '!=', '-1')
                ->where('customer_id',$customerId);
            })
            ->orWhere(function($query) use($search, $customerId){
                $query->where('prd_supplier_item', 'LIKE', '%' . $search . '%')
                ->where('sys_state', '!=', '-1')
                ->where('customer_id',$customerId);
            })
            ->orWhere(function($query) use($search, $customerId){
                $query->where('prd_our_item_no', 'LIKE', '%' . $search . '%')
                ->where('sys_state', '!=', '-1')
                ->where('customer_id',$customerId);
            })
        ->get();
webrobert's avatar

@vandan hang on,

you want products that are related to the customer

that have a supplier that match the search and

that sys_state','!=','-1

does that part work?

$pi = Product::where('customer_id',$customerId)
     ->whereHas('supplier', function (Builder $query) use($search) {
         $query->where('sys_state', '!=', '-1') // this is on both product and on supplier?
               ->orWhere('name','LIKE','%' . $search . '%');
     })
     ->where('sys_state','!=','-1')
     ->get()
vandan's avatar
Level 13

@webrobert both customers and suppliers are related to products sys_state','!=','-1 this is just check that this record is delete or active

vandan's avatar
Level 13

@webrobert yes but when i search particular product description can't search on it

webrobert's avatar

@vandan something like this...

$pi = Product::where('customer_id',$customerId)
    ->whereHas('supplier', function (Builder $query) use($search) {
     $query->where('sys_state', '!=', '-1') 
           ->orWhere('name','LIKE','%' . $search . '%');
    })
    ->where('sys_state','!=','-1')
    ->where(function ($query) use($search) {
        $query->where('prd_our_item_no', 'LIKE', '%' . $search . '%')
              ->orWhere('prd_supplier_item', 'LIKE', '%' . $search . '%')
              ->orWhere('prd_description','LIKE','%' . $search . '%');
    })
    ->get()
1 like
psrz's avatar

@vandan

Wait

$query
   ->where('sys_state', '!=', '-1')
   ->orWhere('name','LIKE','%' . $search . '%');

If sys_state','!=','-1 is to check whether a record is deleted or not, then that is wrong.

If the record is not deleted, that's it, it will be included regardless of name search. That should be a "and"

$query
   ->where('sys_state', '!=', '-1')
   ->where('name','LIKE','%' . $search . '%');

Perhaps you should include that filter as a global scope in the corresponding eloquent model (whatever model the 'suplier' relation refers to) that way this query will be easier to read and mantain.

1 like
vandan's avatar
Level 13

@webrobert products and all things work perfect on search but when i search on supplier cant search on with supplier

vandan's avatar
Level 13

@psrz when i remove orwhere than all the search not working

webrobert's avatar

@vandan oops

$pi = Product::where('customer_id',$customerId)
     ->where('sys_state','!=','-1')
     ->where(function ($q) use($search) {
         $q->whereHas('supplier', function (Builder $query) use($search) {
             $query->where('sys_state', '!=', '-1')
                   ->where('name','LIKE','%' . $search . '%');
         })
           ->orWhere('prd_our_item_no', 'LIKE', '%' . $search . '%')
           ->orWhere('prd_supplier_item', 'LIKE', '%' . $search . '%')
           ->orWhere('prd_description','LIKE','%' . $search . '%');
     })
     ->get()
1 like
vandan's avatar
Level 13

@webrobert hy whole search not working but when i remove orWhere

	$q->whereHas('supplier', function (Builder $query) use($search) {
         $query->where('sys_state', '!=', '-1')
               ->where('name','LIKE','%' . $search . '%');
     })

still work but issue is my deleted supplier also display

psrz's avatar

@vandan

What do you mean the query does not work ? Error ? Not getting the expected results ?

I mean, when you search over the suplier relation: 1) does the record have to be active ? 2) does the record have to match the pattern of name ? If both answers are "yes", then you have to use "and", not "or"

1 like
webrobert's avatar

@vandan good catch, I removed the or on the whereHas...

So it works now?

You just need to remove the deleted supplier ?

$q->whereHas('supplier', function (Builder $query) use($search) {
     $query->where('sys_state', '!=', '-1') // <- thats here right
           ->where('name','LIKE','%' . $search . '%');
 })

Do you need more than one state removed?

1 like
vandan's avatar
Level 13

@webrobert products search works but with supplier name search can't work when i add OrWhere condition & when i remove OrWhere than deleted suppliers show on list

psrz's avatar

@webrobert

That's what I'm refering to.

The deleted suppliers should be filtered. That's a given . But then the name has to match the search. Both things have to be true. That's why can't be "or"

1 like
webrobert's avatar
Level 51

@vandan,

So this doesn't work? (reposted from above)

$pi = Product::where('customer_id',$customerId)
     ->whereNot('sys_state', '-1')
     ->where(function ($q) use($search) {
         $q->whereHas('supplier', function (Builder $query) use($search) {
             $query->where('name','LIKE','%' . $search . '%')
				   ->whereNot('sys_state', '-1');
         })
           ->orWhere('prd_our_item_no', 'LIKE', '%' . $search . '%')
           ->orWhere('prd_supplier_item', 'LIKE', '%' . $search . '%')
           ->orWhere('prd_description','LIKE','%' . $search . '%');
     })
     ->get()

// whereNots just for readability 

@psrz I think we crossed replied. I had corrected the Or.

1 like
vandan's avatar
Level 13

@webrobert my system is down whenever I comeback will check but yes thank you so much for guide and help me much appreciate

Please or to participate in this conversation.