【发布时间】:2019-07-16 11:09:26
【问题描述】:
我有一个带有id、name 和cost 的模型。
protected $table = 'has_costs';
protected $fillable = [
'id','name','cost'
];
然后我还用append添加了新的列,cost_under和cost_over,基本上都是从cost做简单的计算。
protected $appends = ['cost_over','cost_under'];
我是否应该像这样在模型中进行计算:
public function getCostOverAttribute()
{
$costOver = (20/100)*cost;
return $this->attributes['over'] = $costOver;
}
public function getCostUnderAttribute()
{
$costUnder = (80/100)*cost;
return $this->attributes['under'] = $costUndr;
}
或者我还是应该在控制器中做它以保持它更“MVC”?
实际的代码比这个例子更复杂,需要花费大量时间考虑如何在复杂的 Eloquent with 查询中附加每个值。
【问题讨论】:
标签: laravel model-view-controller eloquent model append