Home > Enterprise >  why orWhere Operator not working in laravel
why orWhere Operator not working in laravel

Time:08-03

In my code i want to search by product_name and merchant_name but when i use orWhere operator i get this error

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::orWhere does not exist. in file /Users/admin/Desktop/GSM Source Code/API/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 113

 public function search($name)
{

    $brand = Brand::join('product_categories','product_categories.id','=','brands.category')
    ->join('products','products.id','=','brands.product')
    ->join('merchants','merchants.id','=','brands.merchant')
    ->join('new_shops','new_shops.id','=','brands.shop')
    ->get([
        'brands.id',
        'brands.image',
        'product_categories.category',
        'products.product_name',
        'merchants.merchant_name',
        'new_shops.shop_name',
        'brands.activation',
        'brands.created_at',
        'brands.updated_at'
    ])->where('product_name',$name)
    ->orWhere('merchant_name',$name);

    return new BrandResource($brand);

}

Also when i use LIKE operator in the where clause it doesnot not give any result

CodePudding user response:

you must use where and orWhere after get, like this:

$brand = Brand::join('product_categories','product_categories.id','=','brands.category')
    ->join('products','products.id','=','brands.product')
    ->join('merchants','merchants.id','=','brands.merchant')
    ->join('new_shops','new_shops.id','=','brands.shop')
     ->where('product_name',$name)
    ->orWhere('merchant_name',$name)
    ->get();

CodePudding user response:

orWhere is a QueryBuilder method. If you use get you are returning a collection which means you cannot use it. Try putting them before the get and should work well

  • Related