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; use HasFactory;
/** /**
* The attributes that are mass assignable. * @inheritdoc
*/ */
protected array $fillable = [ protected array $fillable = [
'name', 'name',

View File

@ -11,13 +11,18 @@ class IngredientAmount extends Model
use HasFactory; use HasFactory;
/** /**
* The attributes that are mass assignable. * @inheritdoc
*/ */
protected array $fillable = [ protected array $fillable = [
'amount', 'amount',
'weight', 'weight',
]; ];
/**
* @inheritdoc
*/
protected array $with = ['ingredient'];
/** /**
* Get the Ingredient this amount belongs to. * Get the Ingredient this amount belongs to.
*/ */
@ -31,4 +36,11 @@ class IngredientAmount extends Model
public function recipe(): BelongsTo { public function recipe(): BelongsTo {
return $this->belongsTo(Recipe::class); 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; use HasFactory;
/** /**
* The attributes that are mass assignable. * @inheritdoc
*/ */
protected array $fillable = [ protected array $fillable = [
'name', 'name',
]; ];
/**
* @inheritdoc
*/
protected array $with = ['ingredientAmounts'];
/** /**
* Get the Ingredient Amounts used for this Recipe. * Get the Ingredient Amounts used for this Recipe.
*/ */

View File

@ -12,32 +12,26 @@ class User extends Authenticatable
use HasFactory, Notifiable; use HasFactory, Notifiable;
/** /**
* The attributes that are mass assignable. * @inheritdoc
*
* @var array
*/ */
protected $fillable = [ protected array $fillable = [
'name', 'name',
'email', 'email',
'password', 'password',
]; ];
/** /**
* The attributes that should be hidden for arrays. * @inheritdoc
*
* @var array
*/ */
protected $hidden = [ protected array $hidden = [
'password', 'password',
'remember_token', 'remember_token',
]; ];
/** /**
* The attributes that should be cast to native types. * @inheritdoc
*
* @var array
*/ */
protected $casts = [ protected array $casts = [
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
]; ];
} }