-
app/Models/Ingredients/Ingredient.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; class Ingredient extends Model { // public function getNameAttribute(): string { $name = ''; if ($this->amount !== null) { $name .= "{$this->amount}"; } if ($this->amount_max !== null) { if ($this->amount !== null) { $name .= ' '; } $name .= "- {$this->amount_max}"; } if ($this->unit_id) { $name .= " {$this->unit->name}"; } if ($this->food_id) { $name .= " {$this->food->name}"; } $count = $this->ingredientAttributes()->count(); if (!$count) { return $name; } $this->ingredientAttributes->each(function ($ingredientAttribute, $key) use ($name, $count) { if ($key === 0) { $name .= " ("; } $name .= $ingredientAttribute->name; if ($key === $count) { $name .= ")"; return; } $name .= ", "; }); return $name; } // }
-
resources/js/views/Recipe/Ingredient/Ingredient.vue
Open in GitHub<template> <div> <li> <span @click.prevent="$emit('click', $event)" :class="{'can-edit': editmode.enabled}"> <span v-if="alternateId">{{ $t('Or') }}:</span> {{ ingredient | textify(multiply) }} </span> </li> </div> </template> <script> import { mapState } from "vuex"; export default { props: ["ingredient", "multiplier", "alternateId"], computed: { ...mapState({ editmode: state => state.recipe.editmode.data, }), multiply() { if ( this.multiplier == null || this.multiplier == undefined || this.multiplier == "undefined" ) { return 1; } return this.multiplier; } }, filters: { textify(ingredient, multiply = 1) { let text = ""; if (ingredient.amount !== null) { text += (ingredient.amount * multiply).toPrecision(3); } if (ingredient.amount_max !== null) { if (ingredient.amount !== null) { text += ` `; } text += `- ${(ingredient.amount_max * multiply).toPrecision(3)}`; } if (ingredient.unit !== null) { text += ` ${ingredient.unit.name}`; } if (ingredient.food !== null) { text += ` ${ingredient.food.name}`; } if (ingredient.ingredient_attributes !== []) { ingredient.ingredient_attributes.forEach((attribute, index) => { if (index === 0) { text += " ("; } text += attribute.name; if (index !== ingredient.ingredient_attributes.length - 1) { text += ", "; } if (index === ingredient.ingredient_attributes.length - 1) { text += ")"; } }); } return text; } } }; </script> //