ownedStore($request, $slug); return Inertia::render('stores/products/index', [ 'store' => $this->presentStore($store), 'products' => $store->products() ->with('images') ->latest('id') ->get() ->map(fn (Product $product) => $this->present($product)), ]); } public function create(Request $request, string $slug): Response { return Inertia::render('stores/products/create', [ 'store' => $this->presentStore($this->ownedStore($request, $slug)), 'statuses' => ProductStatus::options(), 'maxImages' => SaveProductRequest::MAX_IMAGES, ]); } public function store(SaveProductRequest $request, string $slug): RedirectResponse { $store = $this->ownedStore($request, $slug); $product = $store->products()->create($request->productData()); $this->addImages($product, $request->file('images', [])); Inertia::flash('toast', ['type' => 'success', 'message' => __('Product added.')]); return to_route('stores.products.index', $store->slug); } public function edit(Request $request, string $slug, int $product): Response { $store = $this->ownedStore($request, $slug); return Inertia::render('stores/products/edit', [ 'store' => $this->presentStore($store), 'product' => $this->present($this->ownedProduct($store, $product)), 'statuses' => ProductStatus::options(), 'maxImages' => SaveProductRequest::MAX_IMAGES, ]); } public function update(SaveProductRequest $request, string $slug, int $product): RedirectResponse { $store = $this->ownedStore($request, $slug); $product = $this->ownedProduct($store, $product); $files = $request->file('images', []); if ($product->images()->count() + count($files) > SaveProductRequest::MAX_IMAGES) { throw ValidationException::withMessages([ 'images' => 'A product can have at most '.SaveProductRequest::MAX_IMAGES.' photos. Remove one first.', ]); } $product->update($request->productData()); $this->addImages($product, $files); Inertia::flash('toast', ['type' => 'success', 'message' => __('Product saved.')]); return to_route('stores.products.index', $store->slug); } public function destroy(Request $request, string $slug, int $product): RedirectResponse { $store = $this->ownedStore($request, $slug); $this->ownedProduct($store, $product)->delete(); Inertia::flash('toast', ['type' => 'success', 'message' => __('Product deleted.')]); return to_route('stores.products.index', $store->slug); } public function destroyImage(Request $request, string $slug, int $product, int $image): RedirectResponse { $store = $this->ownedStore($request, $slug); $product = $this->ownedProduct($store, $product); $product->images()->whereKey($image)->firstOrFail()->delete(); return back(); } /** * @param array $files */ private function addImages(Product $product, array $files): void { $position = (int) $product->images()->max('position'); foreach ($files as $file) { $product->images()->create([ 'store_id' => $product->store_id, 'path' => $file->store("products/{$product->store_id}", 'public'), 'position' => ++$position, ]); } } /** * The product fields the dashboard is allowed to see. Prices are in ringgit, as text, such as "12.90". * * @return array */ private function present(Product $product): array { return [ 'id' => $product->id, 'name' => $product->name, 'slug' => $product->slug, 'description' => $product->description, 'price' => number_format($product->price / 100, 2, '.', ''), 'compare_at_price' => $product->compare_at_price !== null ? number_format($product->compare_at_price / 100, 2, '.', '') : null, 'stock' => $product->stock, 'status' => $product->status->value, 'status_label' => $product->status->label(), 'images' => $product->images ->map(fn (ProductImage $image) => ['id' => $image->id, 'url' => $image->url()]) ->all(), ]; } /** * @return array{name: string, slug: string} */ private function presentStore(Store $store): array { return ['name' => $store->name, 'slug' => $store->slug]; } private function ownedStore(Request $request, string $slug): Store { return $request->user()->stores()->where('slug', $slug)->firstOrFail(); } private function ownedProduct(Store $store, int $id): Product { return $store->products()->with('images')->whereKey($id)->firstOrFail(); } }