/** * Get data formatted for JSON output * * @param Array $attach Optional: sub-resources to attach to the base data, if available. Possible values: options, images, custom_fields * @return Array $output The formatted data */ public function getOutput($attach = []) { $output = array('id' => $this->id, 'name' => $this->name, 'sku' => $this->sku, 'description' => $this->description, 'price' => $this->price, 'cost_price' => $this->cost_price, 'retail_price' => $this->retail_price, 'sale_price' => $this->sale_price, 'calculated_price' => $this->calculated_price, 'warranty' => $this->warranty, 'custom_url' => $this->custom_url); if (in_array('options', $attach) && isset($this->_options)) { $output['options'] = unserialize($this->_options); } if (in_array('images', $attach)) { $images = Image::where('product_id', $this->id)->get(); $output['images'] = []; if (count($images)) { foreach ($images as $image) { $output['images'][] = $image->mapData(); } } } if (in_array('custom_fields', $attach)) { $custom_fields = CustomField::where('product_id', $this->id)->get(); $output['custom_fields'] = []; if (count($custom_fields)) { foreach ($custom_fields as $custom_field) { $output['custom_fields'][] = $custom_field->mapData(); } } } return $output; }