mirror of https://github.com/kcal-app/kcal.git
Add example total calories getter
This commit is contained in:
parent
d54ad13460
commit
790e1af828
|
@ -10,7 +10,7 @@ class Ingredient extends Model
|
|||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'name',
|
||||
|
|
|
@ -11,13 +11,18 @@ class IngredientAmount extends Model
|
|||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'amount',
|
||||
'weight',
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected array $with = ['ingredient'];
|
||||
|
||||
/**
|
||||
* Get the Ingredient this amount belongs to.
|
||||
*/
|
||||
|
@ -31,4 +36,11 @@ class IngredientAmount extends Model
|
|||
public function recipe(): BelongsTo {
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total calories for the ingredient amount.
|
||||
*/
|
||||
public function calories(): float {
|
||||
return $this->ingredient->calories * $this->amount;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,12 +12,17 @@ class Recipe extends Model
|
|||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'name',
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected array $with = ['ingredientAmounts'];
|
||||
|
||||
/**
|
||||
* Get the Ingredient Amounts used for this Recipe.
|
||||
*/
|
||||
|
|
|
@ -12,32 +12,26 @@ class User extends Authenticatable
|
|||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $fillable = [
|
||||
protected array $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $hidden = [
|
||||
protected array $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $casts = [
|
||||
protected array $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue