Fix product resource with comments

This commit is contained in:
Jérémy Munsch 2023-09-11 11:23:42 +02:00
parent 0681bf8f0a
commit 6aab7d4eff
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
2 changed files with 9 additions and 2 deletions

View file

@ -6,6 +6,7 @@ use App\Enums\Products\ClaimEnum;
use App\Enums\Products\OriginEnum;
use App\Models\Keyword;
use App\Models\Product;
use App\Models\ProductComment;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
@ -87,7 +88,13 @@ class ProductResource extends JsonResource
'product_range' => $this->product->product_range->label(),
'geo_area' => $this->product->geo_area->label(),
'freeComment' => $this->product->comment,
'productComments' => $this->product->productComments,
// FIXME: Use dedicaced resources for relations.
'productComments' => $this->product->productComments->map(function (ProductComment $comment) {
return (object)\array_merge($comment->toArray(), [
'created_at' => $comment->created_at->isoFormat('DD\/MM\/YYYY'),
'updated_at' => $comment->updated_at->isoFormat('DD\/MM\/YYYY'),
]);
}),
'productKeywords' => $this->product->productKeywords
->map(fn (Keyword $keyword) => $keyword->label)
->implode(', '),

View file

@ -71,7 +71,7 @@
<p><strong>{{ __('Commentaire libre : ') }}</strong>{{ strip_tags($product->freeComment) }}</p>
@endif
@foreach ($product->productComments as $productComment)
<p><strong>{{ __('Commentaire dégustation du : ') }}</strong>{{ $productComment->created_at->isoFormat('DD\/MM\/YYYY') }}</p>
<p><strong>{{ __('Commentaire dégustation du : ') }}</strong>{{ $productComment->created_at }}</p>
<table class="table" width="100%">
<tr class="table-header">
<td class="table-cell-text-center" width="20%"><strong>{{ __('Appréciation') }}</strong></td>