Laravel将十进制视为字符串 [英] Laravel treating decimal as string

查看:89
本文介绍了Laravel将十进制视为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行下一个雄辩的查询

Im trying to make the next eloquent query

$result = $result->where('money','>=',(float)$moneyFilter);

我的数据库中的money列为DECIMAL(11,2),当我运行查询时,它返回一个空数组,当我经过php artisan tinker并看到列money时,它是一个字符串值"11.1".

The money column in my database its DECIMAL(11,2), when I run the query it returns an empty array, when I go over php artisan tinker and see the column money, it's a string value "11.1".

我想过滤$ result集合,使其值超过$ moneyFilter.

I would like to filter the $result collection to have values over $moneyFilter.

谢谢

推荐答案

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

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

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

这里有一个很好的解释:

There is a really good explanation here:

https://mattstauffer.com/blog/laravel-5.0-雄辩的属性广播/

文档中也有一个解释:

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

这篇关于Laravel将十进制视为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆