getRepositoryInstance()->scopeQuery(function ($query) use ($request) { foreach ($request->except($this->requestException) as $input => $value) { $query = $query->whereIn($input, array_map('trim', explode(',', $value))); } if($menu = $request->input('menu')){ $query = $query->join('menu_brands','brands.id','=','menu_brands.brand_id') ->where('menu_brands.menu_id', $menu); } $query->whereHas('flats', function($q){ $q->where('status',1); if(request()->has('category')) { $q->whereIn('product_id',function ($q) { $q->select('product_categories.product_id')->from('product_categories') ->where('product_categories.category_id',request()->get('category')); }); } if($key = request()->input('search')){ $q->where('name','like', '%'.$key.'%'); } }); if ($sort = $request->input('sort')) { return $query->orderBy($sort, $request->input('order') ?? 'asc'); } return $query->orderBy('position', 'desc')->orderBy('name', 'asc'); }); if (is_null($request->input('pagination')) || $request->input('pagination')) { $results = $query->paginate($request->input('limit') ?? 10); } else { $results = $query->get(); } return $this->getResourceCollection($results); } }