Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

ammannbe/RecipeManager

37 stars
2 code files
View ammannbe/RecipeManager on GitHub

app/Models/Ingredients/Ingredient.php

Open in GitHub
use 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>
//

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.