Edit File: StoreProductResource.php
<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use App\Models\ProductAdditive; use App\Models\ProductAdditiveCategory; class StoreProductResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */ public function toArray($request) { $groups =[]; $productfeatures = $this->productfeatures; if($this->type == 'simple'){ $group = $this->groups()->first(); }else{ $group =$this->groups()->where('properities' , null)->first(); $groups =ProductGroupResource::collection($this->groups()->where('properities','!=' , null)->get()); } $discount_price= null; $addtives = ProductAdditive::where('product_id' , $this->id)->get(); $product_additive_categories = []; foreach ($addtives as $addtive){ $product_additive_category = ProductAdditiveCategory::where('id' , $addtive->product_additive_category_id)->first(); if(in_array($product_additive_category , $product_additive_categories)) { continue; } array_push($product_additive_categories , $product_additive_category); } if(! $request['product_id']) { $request['product_id'] = $this->id; } return [ 'id' => $this->id, 'image' => $this->image?$this->ImagePath:'', 'name_ar' => $this->getTranslation('name' , 'ar'), 'name_en' => $this->getTranslation('name' , 'en'), 'desc_ar' => $this->getTranslation('desc' , 'ar'), 'desc_en' => $this->getTranslation('desc' , 'en'), 'name' => $this->name??'', 'desc' => $this->desc??'', 'type' => $this->type??'', 'available' => $this->available == 'true'? true : false, 'display_price' => $this->display_price()??'0', 'price' => (string)$group->price??'0', 'discount' => $group->discount??0, 'in_stock_sku' => $group->in_stock_sku??'', 'in_stock_qty' => '', 'in_stock_type' => $group->in_stock_type??'', 'price_after_discount' => $discount_price, 'discount_from' => $group->from??'', 'discount_to' => $group->to??'', 'store_menu_category_id' => (int)$this->store_menu_category_id??'', 'store_menu_category_name' => $this->storeMenuCategory?->name ??'', 'features' => FeaturesResource::collection($productfeatures), 'group' => $groups, 'product_additive_categories' => ProductAdditiveCategoryResource::collection($this->additives()->get()), ]; } }
Back to File Manager