mivita/app/Models/Ingredient.php
Kevin Adametz bfa3bb1df4 08 2024
2024-08-05 12:05:24 +02:00

101 lines
3.3 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class Ingredient
*
* @property int $id
* @property string $name
* @property string $trans_name
* @property string $inci
* @property string $trans_inci
* @property string $effect
* @property string $trans_effect
* @property bool $active
* @property int $pos
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Collection|Product[] $products
* @package App\Models
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductIngredient[] $product_ingredients
* @property-read int|null $product_ingredients_count
* @property-read int|null $products_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereEffect($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereInci($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransEffect($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransInci($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereUpdatedAt($value)
* @property-read Collection<int, \App\Models\TransIngredient> $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 : '';
}
}