2025-11-05 17:09:12 +03:00

68 lines
2.3 KiB
PHP

<?php
namespace App\Http\Resources\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'nom' => $this->nom,
'reference' => $this->reference,
'categorie_id' => $this->categorie_id,
'fabricant' => $this->fabricant,
'stock_actuel' => $this->stock_actuel,
'stock_minimum' => $this->stock_minimum,
'unite' => $this->unite,
'prix_unitaire' => $this->prix_unitaire,
'date_expiration' => $this->date_expiration?->format('Y-m-d'),
'numero_lot' => $this->numero_lot,
'conditionnement' => [
'nom' => $this->conditionnement_nom,
'quantite' => $this->conditionnement_quantite,
'unite' => $this->conditionnement_unite,
],
'media' => [
'photo_url' => $this->photo_url,
'fiche_technique_url' => $this->fiche_technique_url,
],
'is_low_stock' => $this->stock_actuel <= $this->stock_minimum,
'created_at' => $this->created_at?->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at?->format('Y-m-d H:i:s'),
// Relations
'fournisseur' => $this->whenLoaded('fournisseur', function() {
return $this->fournisseur ? [
'id' => $this->fournisseur->id,
'name' => $this->fournisseur->name,
'email' => $this->fournisseur->email,
] : null;
}),
'category' => $this->whenLoaded('category', function() {
return $this->category ? [
'id' => $this->category->id,
'name' => $this->category->name,
'code' => $this->category->code,
] : null;
}),
];
}
public function with(Request $request): array
{
return [
'status' => 'success',
'message' => 'Produit récupéré avec succès',
];
}
}