*/ public function toArray(Request $request): array { return [ 'id' => $this->id, 'category_id' => $this->category_id, 'name' => $this->name, 'slug' => $this->slug, 'sku' => $this->sku, 'status' => $this->status->value, 'price' => $this->price, 'compare_at_price' => $this->compare_at_price, 'cost_price' => $this->cost_price, 'photos' => collect($this->photos ?? [])->map(fn (string $p) => ['path' => $p, 'url' => route('api.v1.products.file', ['product' => $this->id, 'column' => 'photos', 'path' => $p])])->all(), 'photos_variants' => collect($this->photos ?? [])->map(fn (string $p) => ImageVariants::urls('api.v1.products.file', ['product' => $this->id, 'column' => 'photos', 'path' => $p], Product::IMAGE_VARIANTS['photos']))->all(), 'video_url' => $this->video_url, 'description' => $this->description, 'size_chart' => $this->size_chart ? route('api.v1.products.file', ['product' => $this->id, 'column' => 'size_chart']) : null, 'is_featured' => $this->is_featured, 'published_at' => $this->published_at?->toIso8601String(), 'published_by' => $this->published_by, 'specs' => $this->specs === null ? null : (object) $this->specs, 'created_at' => $this->created_at?->toIso8601String(), 'updated_at' => $this->updated_at?->toIso8601String(), 'category' => $this->whenLoaded('category', fn () => ['id' => $this->category?->id, 'label' => $this->category?->name]), 'publishedByRef' => $this->whenLoaded('publishedByRef', fn () => ['id' => $this->publishedByRef?->id, 'label' => $this->publishedByRef?->name]), 'productVariants' => $this->whenLoaded('productVariants', fn () => $this->productVariants->map(fn ($item) => ['id' => $item->id, 'name' => $item->name, 'sku' => $item->sku, 'size' => $item->size, 'color' => $item->color, 'price' => $item->price, 'stock' => $item->stock])->all()), 'reviews' => $this->whenLoaded('reviews', fn () => $this->reviews->map(fn ($item) => ['id' => $item->id, 'title' => $item->title, 'rating' => $item->rating, 'status' => $item->status])->all()), 'coupons' => $this->whenLoaded('coupons', fn () => $this->coupons->map(fn ($item) => ['id' => $item->id, 'code' => $item->code, 'description' => $item->description, 'kind' => $item->kind, 'percent_off' => $item->percent_off, 'amount_off' => $item->amount_off, 'starts_on' => $item->starts_on])->all()), 'tags' => $this->whenLoaded('tags', fn () => $this->tags->map(fn ($item) => ['id' => $item->id, 'label' => $item->name])->all()), 'product_variants_count' => $this->whenCounted('productVariants'), 'product_variants_exists' => $this->whenExistsLoaded('productVariants'), 'reviews_count' => $this->whenCounted('reviews'), 'reviews_exists' => $this->whenExistsLoaded('reviews'), 'coupons_count' => $this->whenCounted('coupons'), 'coupons_exists' => $this->whenExistsLoaded('coupons'), 'tags_count' => $this->whenCounted('tags'), 'tags_exists' => $this->whenExistsLoaded('tags'), ]; } }