*/ #[Authorize('view', 'product')] public function show(Request $request, Product $product): array { $product->setAttribute('tag_ids', $product->tags()->pluck('tags.id')->all()); $payload = $this->formPayload($product); // What this editor starts from, so update() can refuse a stale save. $payload['_version'] = RecordVersion::ofEdits($product, ['tags']); if (! $request->boolean('duplicate')) { return $payload; } // A copy is a new record: opening one asks what creating one does. Gate::authorize('create', Product::class); return Arr::except($payload, ['id', 'slug', 'sku', 'photos', 'size_chart', 'created_at']); } /** * Get the record as the form edits it. * * @return array */ private function formPayload(Product $product): array { return [ ...ProductColumns::make()->row($product), 'category_id' => $product->category_id, 'published_at' => $product->published_at?->toIso8601String(), 'published_by' => $product->published_by, 'specs' => $product->specs, 'tag_ids' => $product->tags->pluck('id')->all(), 'description' => $product->description, 'categoryChain' => $this->categoryPickerChain($product->category_id), ]; } /** * Get each level a picker walks, from the chosen row up to its root. * * @return array */ private function categoryPickerChain(?int $leafId): array { if ($leafId === null) { return []; } $option = TreeOptions::option(['table' => 'categories', 'display' => 'name', 'tree' => ['parent' => 'parent_id', 'level' => 'level', 'leaf' => 'category', 'name' => 'name']], $leafId); return $option === null ? [] : ['categories_category' => ['id' => (int) $option['id'], 'label' => $option['label']]]; } }