更改模型中的字段/属性值 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35184646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:10:14  来源:igfitidea点击:

Change field/attribute value in model - Laravel

phplaravel

提问by Sebastian Corneliu V?rlan

I have the table productswith pricecolumn. In model I would like to do something like this:

我有productsprice列的表。在模型中,我想做这样的事情:

public function getPriceAttribute()
{
    return number_format($this->price);
}

So in view I use

所以鉴于我使用

{{ $property->price }} €

and get the value 200instead 200.00how is decimal from database.

并从数据库中获取值200而不是200.00十进制数。

Is this possible?

这可能吗?

采纳答案by Sebastian Corneliu V?rlan

This is what solved my problem:

这就是解决我的问题的原因:

public function getPriceAttribute()
{
    return number_format($this->attributes['price']);
}

This will overwrite the $property->pricevalue (as per comments)

这将覆盖该$property->price值(根据评论)