laravel Eloquent 将十进制转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48288519/
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 17:15:30  来源:igfitidea点击:

Eloquent casts decimal as string

phplaraveleloquentdecimal

提问by Sapnesh Naik

I have a decimal field to represent money in my Eloquent Model and I'm noticing erroneous results when trying to add some number to this field.

我在 Eloquent 模型中有一个十进制字段来表示金钱,并且在尝试向该字段添加一些数字时发现错误的结果。

Upon further inspection, I found that the decimal field is being cast as a string, like "1245114.00"in the tinker console.

经过进一步检查,我发现小数字段被转换为 string,就像"1245114.00"在修补程序控制台中一样。

I checked the table structure and I can verify that the field is indeed decimal(11,3).

我检查了表结构,我可以验证该字段确实是decimal(11,3).

This question was asked beforebut has no answers.

这个问题之前有人问过,但没有答案。

Why is this happening?

为什么会这样?

回答by Luis felipe De jesus Munoz

You need to define in your model which fields need to be cast to a primitive attribute.

您需要在模型中定义哪些字段需要转换为原始属性。

protected $casts = [
    'my_decimal' => 'float',
];

The $castsproperty on your model provides a convenient method of converting attributes to common data types. The $castsproperty should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are: integer, real, float, double, string, boolean, object, array, collection, date, datetime, and timestamp

$casts模型上的属性提供了一种将属性转换为常见数据类型的便捷方法。该$casts属性应该是一个数组,其中键是要转换的属性的名称,值是您希望将列转换为的类型。支持的转换类型有:integer, real, float, double, string, boolean, object, array, collection, date, datetime, 和timestamp

There is a really good explanation here:

这里有一个非常好的解释:

https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/

https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/

Also there is an explanation in the docs:

文档中也有解释:

https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting

https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting

回答by Niels

According to this thread on Laravel's github repository: https://github.com/laravel/framework/issues/11780

根据 Laravel 的 github 存储库上的这个线程:https: //github.com/laravel/framework/issues/11780

It seems to be a PDO driver issue. Automatically casting decimals to strings. People in that thread said that it was a problem with the mysql 5.2 driver and some rolled back to 5.1. If you're on your own server it you'll be able to downgrade. Otherwise you'll have to cast it to float on model level.

这似乎是 PDO 驱动程序问题。自动将小数转换为字符串。该线程中的人说这是mysql 5.2驱动程序的问题,有些回滚到5.1。如果您在自己的服务器上,则可以降级。否则,您将不得不将其投射到模型级别上。

回答by agm1984

I just spent some time analyzing this issue because my Laravel model shows database decimal columns as strings.

我只是花了一些时间来分析这个问题,因为我的 Laravel 模型将数据库十进制列显示为字符串。

The important thing is that adding typecasting to float in the model doesfix it, but if you were like me, you noticed dd(\App\SomeModel::someScope()->get());still shows those decimal columns as strings after adding typecasting in the model.

重要的是在模型添加类型转换来浮动确实可以修复它,但是如果您像我一样,您会注意到dd(\App\SomeModel::someScope()->get());在模型中添加类型转换后仍然将这些十进制列显示为字符串。

When these values are sent to the client, they are integers, so the typecasting did fix the original problem, thus allowing my JavaScript to receive Numbernot String.

当这些值发送到客户端时,它们是整数,因此类型转换确实解决了原始问题,从而允许我的 JavaScript 接收Numbernot String

I am just making this answer so a person doesn't waste time due to focusing on dd()output. I investigated solutions about the PDO driver, and while some perhaps have merit, it isn't a big deal because the real database outputs are cast to the correct type.

我只是在做这个答案,所以一个人不会因为专注于dd()输出而浪费时间。我调查了有关 PDO 驱动程序的解决方案,虽然有些解决方案可能有优点,但这并不是什么大问题,因为真正的数据库输出被转换为正确的类型。

回答by dRamentol

That's the way it should be since PHP has no decimal type. If you cast decimal to float you may loose precision.

这是应该的方式,因为 PHP 没有十进制类型。如果您将小数转换为浮点数,您可能会失去精度。

The best option is leaving it as it is, as strings. Then use bcmath to operate, which is meant to be used with strings.

最好的选择是保持原样,作为字符串。然后使用 bcmath 进行操作,这意味着与字符串一起使用。