authorizeResource(Product::class); } /** * Display a listing of the resource. */ public function index(Request $request): View { /** @var Builder $productModels */ $productModels = Product::query()->with('range'); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $productModels, $search, null, 'name', 'ean_code', ); } $searchFields = \sprintf( '%s, %s ...', Str::ucfirst(__('validation.attributes.name')), __('validation.custom.ean_code'), ); /** Sort columns with a query */ $this->sortQuery($productModels); /** Custom pagination */ $productModels = $this->paginate($productModels); /** @var view-string $view */ $view = 'back::pages.products.index'; return view($view, ['productModels' => $productModels, 'search' => $search, 'searchFields' => $searchFields]); } /** * Display the specified resource. */ public function show(Product $product): View { /** @var view-string $view */ $view = 'back::pages.products.show'; return view($view, ['productModel' => $product]); } /** * Show the form for creating a new resource. */ public function create(Product $product): View | RedirectResponse { $rangeModels = Range::all(); if (! count($rangeModels)) { return to_route('bo.ranges.index') ->with( 'error', 'Vous devez ajouter une gamme avant de pouvoir ajouter un produit' ); } /** @var view-string $view */ $view = 'back::pages.products.create'; return view($view, [ 'productModel' => $product, 'rangeModels' => $rangeModels, ]); } /** * Store a newly created resource in storage. */ public function store(StoreProductRequest $request): RedirectResponse { return DB::transaction(function () use ($request) { $product = new Product(); $product->fill($request->validated()); if ($product->saveOrFail()) { return to_route('bo.products.edit', $product) ->with('success', trans( 'crud.messages.has_been_created', ['model' => Str::singular(__('models.classes.product'))] )); } return back() ->with('error', trans( 'crud.messages.cannot_be_created', ['model' => Str::singular(__('models.classes.product'))] )); }); } /** * Show the form for editing the specified resource. */ public function edit(Product $product): View { $rangeModels = Range::all(); /** @var view-string $view */ $view = 'back::pages.products.edit'; return view($view, [ 'productModel' => $product, 'rangeModels' => $rangeModels, ]); } /** * Update the specified resource in storage. */ public function update(UpdateProductRequest $request, Product $product): RedirectResponse { return DB::transaction(function () use ($request, $product) { $product->fill($request->validated()); if ($product->saveOrFail()) { return to_route('bo.products.edit', $product) ->with('success', trans( 'crud.messages.has_been_updated', ['model' => Str::singular(__('models.classes.product'))] )); } return back() ->with('error', trans( 'crud.messages.cannot_be_updated', ['model' => Str::singular(__('models.classes.product'))] )); }); } /** * Remove the specified resource from storage. */ public function destroy(Product $product): RedirectResponse { return DB::transaction(function () use ($product) { if ($product->deleteOrFail()) { return to_route('bo.products.index') ->with('success', __( 'crud.messages.has_been_deleted', ['model' => Str::singular(__('models.classes.product'))] )); } return back() ->with('error', __( ':crud.messages.cannot_be_deleted', ['model' => Str::singular(__('models.classes.product'))] )); }); } }