'float', 'carbohydrates' => 'float', 'cholesterol' => 'float', 'fat' => 'float', 'protein' => 'float', 'serving_size' => 'float', 'serving_weight' => 'float', 'sodium' => 'float', ]; /** * @inheritdoc */ protected $appends = [ 'serving_size_formatted', 'serving_unit_formatted', 'units_supported', ]; /** * @inheritdoc */ public function toSearchableArray(): array { return [ 'name' => $this->name, 'tags' => $this->tags->pluck('name')->toArray(), 'detail' => $this->detail, 'brand' => $this->brand, 'source' => $this->source, 'notes' => $this->notes, 'calories' => $this->calories, 'cholesterol' => $this->cholesterol, 'sodium' => $this->sodium, 'carbohydrates' => $this->carbohydrates, 'protein' => $this->protein, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } /** * @inheritdoc */ public static function getTagClassName(): string { return Tag::class; } /** * Get the serving size as a formatted string (e.g. 0.5 = 1/2). */ public function getServingSizeFormattedAttribute(): ?string { $result = null; if (!empty($this->serving_size)) { $result = Number::rationalStringFromFloat($this->serving_size); } return $result; } /** * Get the "formatted" serving unit (for custom unit support). */ public function getServingUnitFormattedAttribute(): ?string { // No unit or unit name can be used for e.g. "bell pepper" or "onion" so // the food name will be displayed directly. if (empty($this->serving_unit) && empty($this->serving_unit_name)) { $unit = null; } else { $unit = $this->serving_unit_name ?? $this->serving_unit ?? 'serving'; } return $unit; } }