Edit File: ProductResource.php
<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use App\Http\Resources\FeaturesResource; use App\Http\Resources\GroupResource; use App\Http\Resources\ProductAdditiveCategoryResource; use App\Models\ProductGroup; use App\Models\Favourite; class ProductResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { $user = auth('api')->user(); $productfeatures = $this->productfeatures; if($this->type == 'simple'){ $group = new GroupResource($this->groups()->first()); $quantity =$this->groups()->in_stock_qty ?? 0; }else{ $group = ['id'=>0,'price'=>'0','qty'=>0]; $quantity= 0; } if($user) { if (Favourite::where('user_id' , $user->id)->where('product_id' , $this->id)->first()){ $status = true; }else{ $status = false; } }else{ $status=false; } $product_additive_categories = $this->store?->productAdditiveCategories; return [ 'id' => $this->id, 'image' => $this->image?$this->ImagePath:'', 'name' => $this->name??'', 'type' => $this->type??'', 'features' => FeaturesResource::collection($productfeatures), 'display_price' => $this->display_price()??0, 'group' => $group, 'product_additive_categories' => ProductAdditiveCategoryResource::collection($product_additive_categories), 'desc' => $this->desc??'', 'preparing_time' => $this->preparing_time, 'quantity' => $quantity, 'is_favourite' => $status??'', ]; } }
Back to File Manager