ownedStore($request, $slug); return Inertia::render('stores/collections/form', [ 'store' => ['name' => $store->name, 'slug' => $store->slug], 'collection' => null, 'products' => $this->productChoices($store), ]); } public function store(Request $request, string $slug): RedirectResponse { $store = $this->ownedStore($request, $slug); $data = $this->validated($request, $store); $collection = $store->collections()->create([ 'name' => $data['name'], 'description' => $data['description'] ?? null, 'position' => (int) $store->collections()->max('position') + 1, ]); $this->syncProducts($collection, $data['product_ids'] ?? []); Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection ":name" created.', ['name' => $collection->name])]); return to_route('stores.products.index', $store->slug); } public function edit(Request $request, string $slug, int $collection): Response { $store = $this->ownedStore($request, $slug); $model = $this->ownedCollection($store, $collection); return Inertia::render('stores/collections/form', [ 'store' => ['name' => $store->name, 'slug' => $store->slug], 'collection' => [ 'id' => $model->id, 'name' => $model->name, 'slug' => $model->slug, 'description' => $model->description, 'product_ids' => $model->products->modelKeys(), ], 'products' => $this->productChoices($store), ]); } public function update(Request $request, string $slug, int $collection): RedirectResponse { $store = $this->ownedStore($request, $slug); $model = $this->ownedCollection($store, $collection); $data = $this->validated($request, $store); // The address (slug) stays the same, so menu links and shared links keep working after a rename. $model->fill(['name' => $data['name'], 'description' => $data['description'] ?? null])->save(); $this->syncProducts($model, $data['product_ids'] ?? []); Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection saved.')]); return to_route('stores.products.index', $store->slug); } public function destroy(Request $request, string $slug, int $collection): RedirectResponse { $store = $this->ownedStore($request, $slug); // Products stay; only the group goes. A "shop by" section simply stops showing it. $this->ownedCollection($store, $collection)->delete(); Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection deleted.')]); return to_route('stores.products.index', $store->slug); } /** * @return array */ private function validated(Request $request, Store $store): array { return $request->validate([ 'name' => ['required', 'string', 'min:2', 'max:60'], 'description' => ['nullable', 'string', 'max:500'], 'product_ids' => ['nullable', 'array', 'max:'.self::MAX_PRODUCTS], 'product_ids.*' => ['integer', Rule::exists('products', 'id')->where('store_id', $store->id)], ], [ 'product_ids.*.exists' => __('Choose products from this store.'), ]); } /** * @param array $ids */ private function syncProducts(Collection $collection, array $ids): void { $positions = []; foreach (array_values(array_unique(array_map('intval', $ids))) as $position => $id) { $positions[$id] = ['position' => $position]; } $collection->products()->sync($positions); } /** * @return list */ private function productChoices(Store $store): array { return array_values($store->products()->with('images')->orderBy('name')->get() ->map(fn (Product $product) => [ 'id' => $product->id, 'name' => $product->name, 'image' => $product->images->first()?->url(), ]) ->all()); } private function ownedStore(Request $request, string $slug): Store { return $request->user()->stores()->where('slug', $slug)->firstOrFail(); } private function ownedCollection(Store $store, int $id): Collection { return $store->collections()->with('products')->whereKey($id)->firstOrFail(); } }