File "ProductResourceWithAttributesTest.php"
Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Resources/User/ProductResourceWithAttributesTest.php
File size: 5.13 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Http\Resources\User;
use App\Models\Product;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResourceWithAttributesTest extends JsonResource
{
public function toArray($request)
{
$product_variations = $price_chart = [];
$product_prices = [];
foreach ($this->prices ?? [] as $price){
$combinations = $ranges = [];
foreach ($price->combinations ?? [] as $combination){
$combinations[] = [
'variation_id' => $combination->variation_id,
];
}
foreach ($price->combinations ?? [] as $combination){
$v = $combination->variation;
if($v){
if (isset($product_variations[$v->type]) && in_array($v->value, array_column($product_variations[$v->type], 'value')))
continue;
$product_variations[$v->type][] = ['id' => $v->id, 'value' => $v->value];
}
}
foreach ($price->priceRanges ?? [] as $range){
if ($range->qty_from > 249) {
$ranges[] = [
'qty_from' => '250+',
'qty_to' => '',
'price' => number_format($range->price, 2, '.', ''),
];
} else {
$ranges[] = [
'qty_from' => $range->qty_from,
'qty_to' => $range->qty_to,
'price' => number_format($range->price, 2, '.', ''),
];
}
}
$product_prices[$price->id] = [
'price_id' => $price->id,
'product_sketch' => asset('images/BR-004S-01.svg'),
'supplier_prod_number' => $price->supplier_prod_number,
'min_price' => number_format($price->min_price, 2, '.', ''),
'max_price' => number_format($price->max_price, 2, '.', ''),
'combination' => $combinations,
'ranges' => $ranges,
];
$price_chart[$price->id] = [
'product_number' => $price->supplier_prod_number,
'wight' => number_format($price->shipping_weight, 2, '.', ''),
'length' => $price->length,
'width' => $price->width,
'height' => $price->height,
];
}
// Reviews
foreach ($this->reviews as $review) {
$reviews[] = [
"name" => $review->user?->name,
"rating" => $review->rating,
"comment" => $review->comment,
];
}
//Related Products
$related_products = Product::where('category_id', $this->category_id)
->where('id', '<>', $this->id)
->active()
->limit(4)
->get();
foreach ($related_products as $related_product) {
$p[] = [
'id' => $related_product->id,
'sku' => $related_product->sku,
'slug' => $related_product->slug,
'name' => $related_product->name,
'price_from' => number_format($related_product->price_from, 2, '.', ''),
'price_to' => number_format($related_product->price_to, 2, '.', ''),
'featured_image' => $related_product->featured_image
? [
$related_product->featured_image->url,
$related_product->featured_image->thumbnail,
$related_product->featured_image->preview,
]
: [],
];
}
return [
'id' => $this->id,
'sku' => $this->sku,
'slug' => $this->slug,
'name' => $this->name,
'description' => $this->description,
'category' => $this->category?->name,
'price_from' => number_format($this->price_from, 2, '.', ''),
'price_to' => number_format($this->price_to, 2, '.', ''),
'engraving' => $this->engraving,
'color_engraving' => $this->color_engraving,
'engraving_fee' => number_format($this->engraving_fee, 2, '.', ''),
'featured_image' => $this->featured_image
? [
$this->featured_image->url,
$this->featured_image->thumbnail,
$this->featured_image->preview,
]
: [],
'gallery_images' => $this->gallery_images,
'variations' => $product_variations,
'product_prices' => $product_prices,
'price_chart' => $price_chart,
'reviews' => $reviews ?? [],
'related_products' => $p ?? [],
];
}
}