$this->id, 'name' => $this->name, 'images' => ProductImage::collection($this->images), /* super attributes */ $this->mergeWhen(!empty($this->parent && $this->parent->super_attributes), [ 'super_attributes' => $this->super_attributes($this->parent->super_attributes), ]), ]; }catch (\Exception $ex){ return [ 'id' => $this->id, 'name' => $this->name, 'images' => ProductImage::collection($this->images), ]; } } private function super_attributes($attributes){ if(is_countable($attributes)){ return $attributes->map(function($item, $key){ return [ 'code' => $item->code, 'value' => $this->{$item->code}, 'name' => $item->name ?? $item->admin_name, 'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name ]; })->toArray(); }else{ $item = $attributes; return [ 'code' => $item->code, 'value' => $this->{$item->code}, 'name' => $item->name, 'label' => $item->options->where('id',$this->{$item->code})->first()->admin_name ]; } } }