Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.19% covered (warning)
89.19%
33 / 37
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProductDetailResource
89.19% covered (warning)
89.19%
33 / 37
0.00% covered (danger)
0.00%
0 / 1
8.08
0.00% covered (danger)
0.00%
0 / 1
 toArray
89.19% covered (warning)
89.19%
33 / 37
0.00% covered (danger)
0.00%
0 / 1
8.08
1<?php
2
3namespace App\Http\Resources\V1;
4
5use Illuminate\Http\Request;
6use Illuminate\Http\Resources\Json\JsonResource;
7
8class ProductDetailResource extends JsonResource
9{
10    public function toArray(Request $request): array
11    {
12        return [
13            'id' => $this->id,
14            'name' => $this->name,
15            'slug' => $this->slug,
16            'sku_no' => $this->sku_no,
17            'barcode' => $this->barcode,
18            'thumbnail' => $this->thumbnail ? (str_starts_with($this->thumbnail, 'http') ? $this->thumbnail : asset($this->thumbnail)) : null,
19            'images' => $this->whenLoaded('images', fn () => $this->images->map(fn ($img) => str_starts_with($img->picture, 'http') ? $img->picture : asset($img->picture))->all()),
20            'details' => $this->details,
21            'short_details' => $this->short_details,
22            'price' => (float) $this->price,
23            'selling_price' => (float) ($this->selling_price ?: $this->price),
24            'is_featured' => (bool) $this->is_featured,
25            'has_warranty' => (bool) $this->has_warranty,
26            'video_url' => $this->video_url,
27            'in_stock' => $this->stock_status !== 'out_of_stock',
28            'category' => $this->whenLoaded('category', fn () => $this->category ? [
29                'id' => $this->category->id,
30                'name' => $this->category->name,
31                'slug' => $this->category->slug,
32            ] : null),
33            'variations' => $this->whenLoaded('variations', fn () => $this->variations->map(fn ($v) => [
34                'id' => $v->id,
35                'sku' => $v->sku,
36                'price' => (float) $v->price,
37                'in_stock' => $v->stock_status !== 'out_of_stock' && $v->stock_quantity > 0,
38                'options' => $v->relationLoaded('attributeValues')
39                    ? $v->attributeValues->map(fn ($av) => [
40                        'attribute' => $av->attribute->name ?? null,
41                        'value' => $av->value,
42                    ])->all()
43                    : [],
44            ])->all()),
45            'specs' => $this->whenLoaded('specs', fn () => $this->specs->map(fn ($s) => [
46                'label' => $s->label,
47                'value' => $s->value,
48            ])->all()),
49        ];
50    }
51}