query('search') ?? ''; $model = $request->query('model'); $products = []; $articles = []; $recipes = []; if (is_array($search)) { abort(400); } if (is_string($model)) { return $this->callAction('searchModel', [$model, $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\Http\Response */ public function searchModel(string $modelName, string $search) { $models = []; $modelList = [ 'product' => Str::singular(trans('models.classes.products')), 'news' => Str::singular(trans('models.classes.news')), 'recipe' => Str::singular(trans('models.classes.recipes')) ]; $search = htmlspecialchars($search); 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')); } /** * Get product query search * * Par leur désignation * Par espèce * Par gamme * Par ingrédients * Par atout produits * Par type produit * * @param string $search * @return \Illuminate\Database\Eloquent\Builder */ private function getProductQuery(string $search): Builder { return Product::query()->where('status', true)->where(function ($query) use ($search) { // Par leur désignation. $query->where('label', 'like', "%{$search}%"); // Par ingrédients. $query->orWhere('ingredients', 'like', "%{$search}%"); // Par atout produits. $query->orWhere('advantage', 'like', "%{$search}%"); // Par gamme. $query->orWhereHas('subrange', function (Builder $q) use ($search) { $q->whereHas('range', function (Builder $q) use ($search) { $q->where('label', 'like', "%{$search}%"); }); }); // Par type de produit (sous gamme). $query->orWhereHas('subrange', function (Builder $q) use ($search) { $q->where('label', 'like', "%{$search}%"); }); // Par espèce. if (\strlen($search)) { /** @var \App\Enums\Products\ProductSpeciesEnum|null $specie */ $specie = \collect(ProductSpeciesEnum::toArray()) ->filter(function (ProductSpeciesEnum $specie) use ($search) { return \strpos(Str::lower($specie->label), Str::lower($search)) !== false; })->first(); if ($specie) { $query->orWhere('species', $specie); } } }); } /** * Get news query search * * Par mots dans le titre * Par mots dans le texte * * @param string $search * @return \Illuminate\Database\Eloquent\Builder */ private function getNewsQuery(string $search): Builder { return News::query()->where('status', true)->where(function ($query) use ($search) { $query->where('title', 'like', "%{$search}%"); $query->orWhere('subtitle', 'like', "%{$search}%"); $query->orWhere('content', 'like', "%{$search}%"); $query->orWhere('content2', 'like', "%{$search}%"); }); } /** * Get recipe query search * * Par mots dans le titre * Par mots dans le texte de la recette * * @param string $search * @return \Illuminate\Database\Eloquent\Builder */ private function getRecipeQuery(string $search): Builder { return Advice::query()->where('status', true) ->where('type', '=', AdviceTypeEnum::recipes()->value) ->where(function ($query) use ($search) { $query->where('title', 'like', "%{$search}%"); $query->orWhere('preparation', 'like', "%{$search}%"); $query->orWhere('advice', 'like', "%{$search}%"); $query->orWhere('ingredients', 'like', "%{$search}%"); }); } }