$translations * @property-read int|null $translations_count * @mixin \Eloquent */ class Ingredient extends Model { protected $table = 'ingredients'; protected $casts = [ 'active' => 'bool', 'pos' => 'int' ]; protected $fillable = [ 'name', 'inci', 'effect', 'active', 'pos' ]; public function products() { return $this->belongsToMany(Product::class, 'product_ingredients') ->withPivot('id') ->withTimestamps(); } public function product_ingredients() { return $this->hasMany(ProductIngredient::class, 'product_ingredients', 'id'); } public function translations() { return $this->hasMany(TransIngredient::class, 'ingredient_id'); } public function getLang($key) { $lang = \App::getLocale(); if ($lang == 'de') { return $this->{$key}; } $trans = $this->getTrans($key, $lang); return $trans != '' ? $trans : $this->{$key}; } public function getTrans($key, $lang) { $trans = $this->translations->where('language','=', $lang)->where('key', $key)->first(); return $trans ? $trans->value : ''; } }