authorizeResource(Product::class); } /** * Display a listing of the resource. */ public function index(Request $request): View { /** @var \Illuminate\Database\Eloquent\Builder $productModels */ $productModels = Product::query()->with('ratings')->withAvg('ratings', 'rate'); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $productModels, $search, null, 'label', ); } $searchFields = \implode(', ', [ trans('validation.custom.label'), ]); /** Sort columns with a query */ $this->sortQuery($productModels); /** Custom pagination */ $productModels = $this->paginate($productModels); return view('bo.pages.products.index', compact('productModels', 'search', 'searchFields')); } /** * Show the specified resource. */ public function show(Product $product): View { return view('bo.pages.products.show', ['productModel' => $product]); } /** * Show the form for creating a new resource. */ public function create(Product $product): View { return view('bo.pages.products.create', [ 'productModel' => $product, 'rangeModels' => $this->getModelsPaginate((new Range())->query()), 'subrangeModels' => $this->getModelsPaginate((new Subrange())->query()), 'meatModels' => $this->getModelsPaginate((new Meat())->query()), 'submeatModels' => $this->getModelsPaginate((new Submeat())->query()), 'approachDemarchModels' => $this->getModelsPaginate( Approach::query()->where('type', ApproachTypeEnum::demarche), 'name' ), 'approachLabelModels' => $this->getModelsPaginate( Approach::query()->where('type', ApproachTypeEnum::label), 'name' ), 'productTypeModels' => $this->getModelsPaginate((new ProductType())->query()), 'claimModels' => $this->getModelsPaginate((new Claim())->query()), ]); } /** * Store a newly created resource in storage. */ public function store(StoreProductRequest $request): RedirectResponse { return DB::transaction(function () use ($request) { $product = new Product(); $product->fill(Arr::except( $request->validated(), ['labels', 'productTypes', 'claims', 'nutritionnal_values'] )); if ($product->saveOrFail()) { $product->labels()->sync(collect($request->validated()['labels'] ?? [])->pluck('id')); $product->productTypes()->sync(collect($request->validated()['productTypes'] ?? [])->pluck('id')); $product->claims()->sync(collect($request->validated()['claims'] ?? [])->pluck('id')); $nutritionnalValueModel = new IncoNutritionalValue(); $nutritionnalValueModel->fill(Arr::first($request->safe()->only('nutritionnal_values'))); $nutritionnalValueModel->product()->associate($product); if ($nutritionnalValueModel->saveOrFail()) { return redirect()->route('bo.products.edit', $product) ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); } } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); }); } /** * Show the form for editing the specified resource. */ public function edit(Product $product): View { return view('bo.pages.products.edit', [ 'productModel' => $product, 'nutritionnalValueModel' => $product->getNutritionnalValue(), 'rangeModels' => $this->getModelsPaginate((new Range())->query()), 'subrangeModels' => $this->getModelsPaginate((new Subrange())->query()), 'meatModels' => $this->getModelsPaginate((new Meat())->query()), 'submeatModels' => $this->getModelsPaginate((new Submeat())->query()), 'approachDemarchModels' => $this->getModelsPaginate( Approach::query()->where('type', ApproachTypeEnum::demarche), 'name' ), 'approachLabelModels' => $this->getModelsPaginate( Approach::query()->where('type', ApproachTypeEnum::label), 'name' ), 'productTypeModels' => $this->getModelsPaginate((new ProductType())->query()), 'claimModels' => $this->getModelsPaginate((new Claim())->query()), ]); } /** * Update the specified resource in storage. */ public function update(UpdateProductRequest $request, Product $product): RedirectResponse { return DB::transaction(function () use ($request, $product) { $product->fill(Arr::except( $request->validated(), ['labels', 'productTypes', 'claims', 'nutritionnal_values'] )); $product->labels()->sync(collect($request->validated()['labels'] ?? [])->pluck('id')); $product->productTypes()->sync(collect($request->validated()['productTypes'] ?? [])->pluck('id')); $product->claims()->sync(collect($request->validated()['claims'] ?? [])->pluck('id')); $nutritionnalValueModel = $product->getNutritionnalValue(); if (is_null($nutritionnalValueModel)) { $nutritionnalValueModel = new IncoNutritionalValue(); $nutritionnalValueModel->product()->associate($product); } $nutritionnalValueModel->fill(Arr::first($request->safe()->only('nutritionnal_values'))); if ($product->saveOrFail() && $nutritionnalValueModel->saveOrFail()) { return redirect()->route('bo.products.edit', $product) ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); } return redirect()->route('bo.products.edit', $product) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); }); } /** * Remove the specified resource from storage. */ public function destroy(Product $product): RedirectResponse { /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($product->deleteOrFail()) { return redirect()->route('bo.products.index', $previousQueries) ->with('success', trans('crud.messages.has_been_deleted', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucfirst(trans('models.classes.product')), ])); } /** * Duplicate the specified resource. */ public function duplicate(Product $product): View { return $this->create($product->replicate()); } /** * Undocumented function */ private function getModelsPaginate( Builder $query, string $fieldName = 'label' ): LengthAwarePaginator { return $query->where('published', true) ->orderBy($fieldName, 'ASC') ->paginate(10); } }