Files
Malek TellissiandClaude Sonnet 4.6 7c0fde4fd5
Deploy / deploy (push) Successful in 14s
Update LOT API to use nested batch_origin_entries structure
Each spice now returns an `origins` array (was single `origin` object) with
supplier and farmer resolved from the new entries relation, not flat columns.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-12 20:07:53 +02:00

51 lines
1.9 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class LotResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'lot_number' => $this->lot_number,
'produced_at' => $this->produced_at?->toDateString(),
'expires_at' => $this->expires_at?->toDateString(),
'units_produced' => $this->units_produced,
'quantity_g' => (float) $this->quantity_g,
'product' => [
'name' => $this->product->title,
'description' => $this->product->description,
],
'packing' => $this->size ? [
'name' => $this->size->packing->name,
'size_g' => (float) $this->size->packing->size,
'sku' => $this->size->sku,
'gtin' => $this->size->gtin,
] : null,
'recipe' => [
'version' => $this->recipe->version,
'created_at' => $this->recipe->created_at->toDateString(),
],
'spices' => $this->recipe->spices->map(function ($spice) {
$origin = $this->origins->firstWhere('spice_id', $spice->id);
return [
'name' => $spice->name,
'percentage' => (float) $spice->pivot->amount_in_percent,
'origins' => $origin
? $origin->entries->map(fn ($e) => [
'country' => $e->origin_country,
'supplier' => $e->supplier?->name,
'farmer' => $e->farmer?->name,
'notes' => $e->notes,
])->values()
: [],
];
})->values(),
];
}
}