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;
|
use HasFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that are mass assignable.
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
protected array $fillable = [
|
protected array $fillable = [
|
||||||
'name',
|
'name',
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue