【问题标题】:Laravel number comparison in 'where' not working'where'中的Laravel数字比较不起作用
【发布时间】:2016-12-03 12:43:19
【问题描述】:

我有一个 Laravel 5.1 应用程序,但在使用数字比较的“位置”时遇到了问题。 具体来说,我正在尝试做:

{{\App\Items::all()->where('paid_price','>',0)->count()}}

paid_price 的 SQL 'type' 是 'decimal(8,2)'。 有几个 Item 行的paid_price 实际上大于零,但上面的代码只产生 0。像下面这样不依赖数字比较的东西工作得很好——你能给我一些关于为什么 > 不不行吗?非常感谢

{{\App\Items::all()->where('other_column','some_value')->count()}}

我的 Items 类的代码如下:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Items extends Model {
protected $fillable =['gateway','paid_price','payment_date','auth_date','charge_date','refunded_date'];
protected $dates = ['payment_date','auth_date','charge_date','refunded_date'];
public function setUserIdAttribute($value)
{
    $this->attributes['user_id'] = $value ?: null;
}
}

【问题讨论】:

    标签: php sql laravel laravel-5 laravel-5.1


    【解决方案1】:

    where 子句中的比较应该可以正常工作。只需删除all()

    \App\Items::where('paid_price','>',0)->count()
    

    如果你指定all(),它会先返回一个Items对象的数组,所以当你调用where()函数时,会导致对返回数组的查询错误

    【讨论】:

      【解决方案2】:

      尝试使用方法toSql() 调试 SQL,以便您可以看到 SQL 查询,并可以从客户端或 MySQL Workbench 等前端执行它:

      {{ \App\Items::all()->where('paid_price','>',0)->count()->toSql() }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        相关资源
        最近更新 更多