query('search') ?? ''); $model = $request->query('model'); $products = []; $productsTotal = 0; $news = []; $newsTotal = 0; $recipes = []; $recipesTotal = 0; if (is_string($model)) { return $this->searchModel($model, $search); } if (!empty($search)) { $productsQuery = $this->getProductQuery($search); $productsTotal = $productsQuery->count(); $products = $productsQuery->with('subrange.range')->limit(3)->get(); $newsQuery = $this->getNewsQuery($search); $newsTotal = $newsQuery->count(); $news = $newsQuery->with('categories')->limit(3)->get(); $recipesQuery = $this->getRecipeQuery($search); $recipesTotal = $recipesQuery->count(); $recipes = $recipesQuery->with('tags')->limit(3)->get(); } return view('fo.pages.search-summary-list', compact( 'products', 'productsTotal', 'news', 'newsTotal', 'recipes', 'recipesTotal', 'search', )); } /** * View search for a specific model * * Pour des raisons de performance et de pertinence la recherche ne pourra être lancé que si le champ de recherche * contient au moins 3 caractères autre que des espaces. Les résultats seront présentés par ordre de pertinence. * * @param string $modelName * @param string $search * @return \Illuminate\Contracts\View\View */ public function searchModel(string $modelName, string $search): \Illuminate\Contracts\View\View { $models = []; $modelList = [ 'product' => Str::singular(trans('models.classes.products')), 'news' => Str::singular(trans('models.classes.news')), 'recipe' => Str::singular(trans('models.classes.recipes')) ]; switch ($modelName) { case 'product': $models = $this->getProductQuery($search)->with('subrange.range')->paginate(6); break; case 'news': $models = $this->getNewsQuery($search)->with('categories')->paginate(6); break; case 'recipe': $models = $this->getRecipeQuery($search)->with('tags')->paginate(6); break; default: abort(400); } //end switch return view('fo.pages.search', compact('modelName', 'models', 'search', 'modelList')); } }