Laravel处理数字作为字符串
问题描述:
我试着使下雄辩查询Laravel处理数字作为字符串
$result = $result->where('money','>=',(float)$moneyFilter);
我的数据库中的钱列十进制(11,2),当我运行该查询返回一个空数组,当我通过php工匠修补程序并查看列金额,这是一个字符串值“11.1”。
我想过滤$ result集合,使其值超过$ moneyFilter。
谢谢
答
您需要在您的模型中定义哪些字段需要转换为原始属性。
protected $casts = ['my_decimal' => 'float'];
这里有一个很好的解释:
https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/
也有是在文档的解释:
https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting
Eloquents [存取器(https://开头laravel.com/docs/5.4/eloquent-mutators#attribute-casting)及其属性强制转换在这种情况下可能会有所帮助。 – milo526
也许......但是为什么它是这样呢? – pilat