更改模型中的字段/属性值 - 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
Change field/attribute value in model - Laravel
提问by Sebastian Corneliu V?rlan
I have the table products
with price
column. In model I would like to do something like this:
我有products
带price
列的表。在模型中,我想做这样的事情:
public function getPriceAttribute()
{
return number_format($this->price);
}
So in view I use
所以鉴于我使用
{{ $property->price }} €
and get the value 200
instead 200.00
how 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->price
value (as per comments)
这将覆盖该$property->price
值(根据评论)