Relations orWhereDoesntHave dont work, how to fix?

Viewed 66

There is a relations "shop" with "setting_limits". Need select all the products that fall under all the settings. The store can set the sales settings, or it can not set, then you need to show all the products of this store.

 ->whereHas('shop', function ($query) {
                        $query->whereHas('setting_limits', function ($query) {
                            $query->where(function ($q) {
                                $q->where('type', '=' ,'sell_selected_countries')
                                    ->where('county_id', '=', 4);
                            })
                                ->orWhere(function ($q) {
                                    $q->where('type', '=' , 'sale_certain_countries')
                                        ->where('county_id', '!=', 4);
                                })
                                ->orWhere(function ($q) {
                                    $q->where('type',  '=' ,'sale_certain_areas')
                                        ->where('area_id', '!=', 5);
                                })
                                ->orWhere(function ($q) {
                                    $q->where('type', '=' , 'sell_selected_areas')
                                        ->where('area_id', '=', 5);
                                });
                        })->orWhereDoesntHave('setting_limits');
                    })

I tried this, it don't work, she does not see the products of the store that have no sales limit.

1 Answers

As per my understanding shop has relationship setting_limits .So you can do somethink like below

->whereHas('shop.setting_limits', function ($query) {

            $query->where(function ($q) {
                $q->where('type', '=' ,'sell_selected_countries')
                    ->where('county_id', '=', 4);
            })->orWhere(function ($q) {
                    $q->where('type', '=' , 'sale_certain_countries')
                        ->where('county_id', '!=', 4);
                })->orWhere(function ($q) {
                    $q->where('type',  '=' ,'sale_certain_areas')
                        ->where('area_id', '!=', 5);
                })->orWhere(function ($q) {
                    $q->where('type', '=' , 'sell_selected_areas')
                        ->where('area_id', '=', 5);
                });
        })->orWhereDoesntHave('shop.setting_limits');
Related