authorizeResource(Recipe::class); } /** * Display a listing of the resource. */ public function index(Request $request): View { /** @var \Illuminate\Database\Eloquent\Builder $recipeModels */ $recipeModels = Recipe::query()->with('ratings')->withAvg('ratings', 'rate'); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $recipeModels, $search, null, 'title', 'tips', 'preparation', 'advice', ); } $searchFields = \implode(', ', [ trans('validation.attributes.title'), trans('validation.custom.tips'), trans('validation.custom.preparation'), trans('validation.custom.advice'), ]); /** Sort columns with a query */ $this->sortQuery($recipeModels); /** Custom pagination */ $recipeModels = $this->paginate($recipeModels); return view('bo.pages.recipes.index', compact('recipeModels', 'search', 'searchFields')); } /** * Show the specified resource. */ public function show(Recipe $recipe): View { $recipe->load('consumptionMoments'); $productModels = Product::query()->where('published', true)->get(); return view('bo.pages.recipes.show', [ 'recipeModel' => $recipe, 'productModels' => $productModels, ]); } /** * Show the form for creating a new resource. */ public function create(Recipe $recipe): View { $productModels = Product::query()->where('published', true)->orderBy('label', 'ASC')->paginate(12); $videoModels = Video::query()->where('published', true)->orderBy('order', 'ASC')->paginate(10); $consumptionMomentModels = ConsumptionMoment::query()->orderBy('title', 'ASC')->paginate(10); return view('bo.pages.recipes.create', [ 'recipeModel' => $recipe, 'productModels' => $productModels, 'consumptionMomentModels' => $consumptionMomentModels, 'videoModels' => $videoModels, ]); } /** * Store a newly created resource in storage. */ public function store(StoreRecipeRequest $request): RedirectResponse { return DB::transaction(function () use ($request) { $recipe = new Recipe(); $recipe->fill(Arr::except($request->validated(), ['products', 'consumption_moments'])); if ($recipe->saveOrFail()) { $recipe->products()->sync($request->products); $recipe->consumptionMoments()->sync($request->validated('consumption_moments', [])); return redirect()->route('bo.recipes.edit', $recipe) ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); }); } /** * Show the form for editing the specified resource. */ public function edit(Recipe $recipe): View { $productModels = Product::query()->where('published', true)->orderBy('label', 'ASC')->paginate(12); $videoModels = Video::query()->where('published', true)->orderBy('title', 'ASC')->paginate(10); $consumptionMomentModels = ConsumptionMoment::query()->orderBy('title', 'ASC')->paginate(10); return view('bo.pages.recipes.edit', [ 'recipeModel' => $recipe, 'productModels' => $productModels, 'consumptionMomentModels' => $consumptionMomentModels, 'videoModels' => $videoModels, ]); } /** * Update the specified resource in storage. */ public function update(UpdateRecipeRequest $request, Recipe $recipe): RedirectResponse { return DB::transaction(function () use ($request, $recipe) { $recipe->fill(Arr::except($request->validated(), ['products', 'consumption_moments'])); $recipe->products()->sync([]); $recipe->products()->sync($request->products); $recipe->consumptionMoments()->sync($request->consumption_moments ?? []); if ($recipe->saveOrFail()) { return redirect()->route('bo.recipes.edit', $recipe) ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); } return redirect()->route('bo.recipes.edit', $recipe) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); }); } /** * Remove the specified resource from storage. */ public function destroy(Recipe $recipe): RedirectResponse { /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($recipe->deleteOrFail()) { return redirect()->route('bo.recipes.index', $previousQueries) ->with('success', trans('crud.messages.has_been_deleted', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucfirst(trans('models.classes.recipe')), ])); } /** * Duplicate the specified resource. */ public function duplicate(Recipe $recipe): View { return $this->create($recipe->replicate()); } /** * Change recipe highlight. * * @return \Illuminate\Http\RedirectResponse */ public function changeHighlight(Recipe $recipe) { /** @var \App\Models\Recipe|null $recipeOld */ $recipeOld = Recipe::query()->where('highlight', true)->first(); if ($recipeOld && $recipeOld->getkey() !== $recipe->getkey()) { $recipeOld->highlight = false; $recipeOld->saveQuietly(); } $recipe->highlight = true; $recipe->saveQuietly(); return redirect()->back()->with('success', trans('crud.messages.highlight_status_saved')); } }