/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(AdFilters $filters, $slug = null, $make_slug = null, $model_slug = null) { if (Request::has('category')) { $getParameters = '?'; foreach (Request::except(['category', 'make', 'model']) as $key => $value) { $getParameters .= $value == '' ? '' : "{$key}={$value}&"; } $getParameters = substr($getParameters, 0, -1); $redirectPath = '/category/' . Request::input('category'); if (Request::has('make') && Request::input('make') != 'any') { $redirectPath .= '/' . Request::input('make'); if (Request::has('model') && Request::input('model') != 'any') { $redirectPath .= '/' . Request::input('model'); } } return redirect($redirectPath . $getParameters); } $builder = Ad::filter($filters)->orderBy('created_at', 'desc')->with(['auto_models', 'favorite']); if ($slug) { $category = Category::where('slug', $slug)->firstOrFail(); if ($category->parent_id) { $builder->where('category_id', $category->id); } else { $categories_ids = $category->childs()->select('id')->get(); $builder->whereIn('category_id', $categories_ids); } if ($make_slug) { if (!$model_slug) { $make_id = AutoModel::where('slug', $make_slug)->select('id')->firstOrFail()->id; $builder->whereHas('auto_models', function ($query) use($make_id) { $query->where('auto_models.make_id', $make_id); }); } else { $model_id = AutoModel::where('slug', $model_slug)->select('id')->firstOrFail()->id; $builder->whereHas('auto_models', function ($query) use($model_id) { $query->where('auto_models.id', $model_id); }); } } } $ads = $builder->paginate(30)->setPath(Request::url())->appends(Request::except('page')); // dd($slug, Category::where('slug', $slug)->first()); $data = ['ads' => $ads, 'regions' => Region::getAllWithCities()]; if (isset($category)) { $data['category'] = $category; $data['title'] = $category->name; } return view('mainpage', $data); }