feat: optimize category and product resource queries with eager loading and response formatting improvements
This commit is contained in:
@@ -26,7 +26,7 @@ class CategoryController extends Controller
|
|||||||
})
|
})
|
||||||
->with(['parent', 'subCategories', 'tenant'])
|
->with(['parent', 'subCategories', 'tenant'])
|
||||||
->orderByDesc('id')
|
->orderByDesc('id')
|
||||||
->paginateFromRequest()
|
->get()
|
||||||
)->response();
|
)->response();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ class ProductController extends Controller
|
|||||||
return ProductResource::collection(
|
return ProductResource::collection(
|
||||||
Product::query()
|
Product::query()
|
||||||
->where('tenant_codigo', $tenant->codigo)
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
->with(['attachments', 'brand', 'category'])
|
->with([
|
||||||
|
'attachments' => fn ($query) => $query->orderBy('attachments.id')->limit(1),
|
||||||
|
'brand',
|
||||||
|
'category',
|
||||||
|
])
|
||||||
->latest()
|
->latest()
|
||||||
->paginateFromRequest()
|
->paginateFromRequest()
|
||||||
)->response();
|
)->response();
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ class AttributeResource extends JsonResource
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'tenant_codigo' => $this->tenant_codigo,
|
|
||||||
'codigo' => $this->codigo,
|
'codigo' => $this->codigo,
|
||||||
'nombre' => $this->nombre,
|
'nombre' => $this->nombre,
|
||||||
'is_required' => $this->is_required,
|
'is_required' => $this->is_required,
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Domains\Catalog\Resources;
|
namespace App\Domains\Catalog\Resources;
|
||||||
|
|
||||||
use App\Domains\Catalog\Resources\BrandResource;
|
|
||||||
use App\Domains\Catalog\Resources\CategoryResource;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
@@ -20,18 +18,23 @@ class ProductResource extends JsonResource
|
|||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'tenant_codigo' => $this->tenant_codigo,
|
'tenant_codigo' => $this->tenant_codigo,
|
||||||
'categoria_id' => $this->categoria_id,
|
'category_id' => $this->categoria_id,
|
||||||
'brand_id' => $this->brand_id,
|
'brand_id' => $this->brand_id,
|
||||||
'slug' => $this->slug,
|
'slug' => $this->slug,
|
||||||
'nombre' => $this->nombre,
|
'nombre' => $this->nombre,
|
||||||
'descripcion' => $this->descripcion,
|
'descripcion' => $this->descripcion,
|
||||||
'precio' => $this->precio,
|
'precio' => $this->precio,
|
||||||
'category' => CategoryResource::make($this->whenLoaded('category')),
|
'category' => $this->whenLoaded('category', fn () => $this->category?->nombre),
|
||||||
'brand' => BrandResource::make($this->whenLoaded('brand')),
|
'brand' => $this->whenLoaded('brand', fn () => $this->brand?->nombre),
|
||||||
'images' => $this->whenLoaded('attachments', fn () =>
|
'images' => $this->whenLoaded('attachments', fn () =>
|
||||||
$this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
$this->attachments
|
||||||
|
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
||||||
|
->values()
|
||||||
|
),
|
||||||
|
'variants' => $this->when(
|
||||||
|
$request->route()?->getName() !== 'productos.index',
|
||||||
|
fn () => ProductVariantResource::collection($this->whenLoaded('variants'))
|
||||||
),
|
),
|
||||||
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
|
|
||||||
'created_at' => $this->created_at,
|
'created_at' => $this->created_at,
|
||||||
'updated_at' => $this->updated_at,
|
'updated_at' => $this->updated_at,
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user