Add example total calories getter

This commit is contained in:
Christopher C. Wells 2020-12-21 19:38:58 -08:00
parent d54ad13460
commit 790e1af828
4 changed files with 26 additions and 15 deletions

View File

@ -10,7 +10,7 @@ class Ingredient extends Model
use HasFactory;
/**
* The attributes that are mass assignable.
* @inheritdoc
*/
protected array $fillable = [
'name',

View File

@ -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;
}
}

View File

@ -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.
*/

View File

@ -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',
];
}