feat: Implement full CRUD API for product categories and products, including recursive product counts for categories.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Admin;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductCategoryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'parent_id' => $this->parent_id,
|
||||
'is_active' => (bool) $this->is_active,
|
||||
'sort_order' => $this->sort_order,
|
||||
'type' => $this->type,
|
||||
'type_label' => $this->type_label,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'children' => $this->whenLoaded('children', fn () =>
|
||||
self::collection($this->children)
|
||||
),
|
||||
'products_count' => $this->whenCounted('products', fn () =>
|
||||
$this->recursive_products_count
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user