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(); // dd($this->groups()->where('properities','!=' , null)->get()); $groups =ProductGroupResource::collection($this->groups()->where('properities','!=' , null)->get()); } if($group->discount_price == null) { $discount_price= null; }else{ $discount_price = $group->discount_price; } $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, 'preparing_time' => $this->preparing_time, 'price' => $group->price??0, 'in_stock_sku' => $group->in_stock_sku??'', 'in_stock_qty' => $group->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??'', 'features' => FeaturesResource::collection($productfeatures), 'group' => $groups, 'product_additive_categories' => ProductAdditiveCategoryResource::collection($product_additive_categories), ]; } }
Back to File Manager