Home > Net >  BadMethodCallException: Method Illuminate\Support\Collection::OrWhereIn does not exist
BadMethodCallException: Method Illuminate\Support\Collection::OrWhereIn does not exist

Time:10-04

In Laravel 5.8 At controller I have this code

$products = DB::table('products as p')
            ->select(
                DB::raw('p.id AS product_id'),
                DB::raw('p.name AS product_name_ar'),
                DB::raw('p.type AS product_type'),
                DB::raw('p.category_id AS product_category_id'),
                DB::raw('p.sub_category_id AS product_subCategory_id'),
                DB::raw('IF(p.product_custom_field1 IS NULL or p.product_custom_field1 = "", p.name , p.product_custom_field1) as product_name_en'),
                DB::raw('p.product_description AS product_description_ar'),
                DB::raw('p.product_custom_field2 AS product_description_eng'),
                DB::raw('p.image AS product_image'),
                DB::raw('p.product_custom_field4 AS default_product'),
                DB::raw('v.id AS variation_id'),
                DB::raw('v.name AS variation_name'),
                DB::raw('v.sub_sku AS sku'),
                DB::raw('v.combo_variations AS combo_variations'),
                // DB::raw('c1.name AS category_name'),
                // DB::raw('c2.name AS sub_category_name'),
                // DB::raw('v.default_purchase_price AS v_dpp'),
                // DB::raw('v.dpp_inc_tax AS v_dpp_inc_tax'),
                // DB::raw('v.default_sell_price as v_dsp'),
                DB::raw('v.sell_price_inc_tax AS v_dsp_inc_tax')
                // DB::raw('p.enable_stock AS product_enable_stock')
            )
            ->leftjoin('variations as v','v.product_id', '=', 'p.id')
            // ->leftjoin('categories as c1','p.category_id', '=', 'c1.id')
            // ->leftjoin('categories as c2','p.sub_category_id', '=', 'c2.id')
            ->leftjoin('product_locations as l','l.product_id', '=', 'p.id')
            ->leftjoin('business_locations as bl','bl.id', '=', 'l.location_id')
            ->where('p.business_id', $business_id)
            ->groupBy('v.id')
            ->orderBy('p.id', 'asc')
            ->get();

Then I need to filter the result according to array of id so I use this code

$products_data['lights_products'] = $products->whereIn('product_category_id', $parties_lights_ids_array)->values();

and I returned correct data after filter, but If I used orWhereIn like this code code

$products_data['lights_products'] = $products->whereIn('product_category_id', $parties_lights_ids_array)->OrWhereIn('product_subCategory_id', $parties_lights_ids_array)->values();

I get this error

BadMethodCallException: Method Illuminate\Support\Collection::OrWhereIn does not exist. in file /home/adfhouse/ta.adf.house/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 104

I tried to

composer dumpautoload

and try also to delete laravel code and install it again by using

rm -rf vendor/laravel

then

composer update

but similar problem

What can I do? Thanks in advance

CodePudding user response:

First of all, your database query is returning an instance of Collection and laravel collection doesn't have any method named OrWhereIn. Here is a list of available methods for collections https://laravel.com/docs/9.x/collections#available-methods

In your case, you can use filter method with custom callback function that will filter your data based on your requirements.

https://laravel.com/docs/9.x/collections#method-filter

For example:

$filtered = $products->filter(function ($product) use ($parties_lights_ids_array) {
    return in_array($product->product_category_id, $parties_lights_ids_array) ||
        in_array($product->product_subCategory_id, $parties_lights_ids_array);
});
  • Related