Home > Blockchain >  Laravel search system
Laravel search system

Time:12-13

   Database

product_name status product_code tag
Product 1 1 PDT1 new
Product 2 0 PDT2 new
Product 3 1 PDT3 new

I want those data where the status is 1. But that code not working properly. It returns all data. Even status is 0. But I want those data where the status is 1. But how can I do this?

                 $search_data = Product::where('product_name','LIKE',"%{$search}%")
                               ->orWhere('tag','LIKE',"%{$search}%")
                               ->orWhere('product_code',$search)
                               ->whereStatus(1)
                              ->get();
                  return $search_data; 

CodePudding user response:

You shoud wrap the search inside the where closure.

$search_data = Product::query()
        ->whereStatus(1)
        ->where(function($query) use ($search){
            return $query->where('product_name', 'LIKE', "%{$search}%")
        ->orWhere('tag', 'LIKE', "%{$search}%")
        ->orWhere('product_code', $search);
        })->get()
                  return $search_data; 

CodePudding user response:

try this

return Product::Where('status',1)->Where(function($query) use ($search){
$query->where('product_name','LIKE',"%{$search}%")
    ->orWhere('tag','LIKE',"%{$search}%")
    ->orWhere('product_code',$search)
}
->get();
            
  • Related