'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' ]; /** * @inheritdoc */ public function toSearchableArray(): array { $this->tags; return [ 'id' => $this->id, 'name' => $this->name, 'detail' => $this->detail, 'brand' => $this->brand, 'tags' => $this->tags->pluck('name'), ]; } /** * Get the serving size as a formatted string (e.g. 0.5 = 1/2). */ public function getServingSizeFormattedAttribute(): string { return Number::fractionStringFromFloat($this->serving_size); } /** * 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; } }