Php 具有多个where子句的Laravel查询范围返回不正确的数据

Php 具有多个where子句的Laravel查询范围返回不正确的数据,php,laravel,laravel-6,Php,Laravel,Laravel 6,我有下表控制台: Schema::创建('consols',函数(Blueprint$表){ $table->bigIncrements('id'); $table->double('current_weight',8,3)->默认值(0); $table->double('current_weight',8,3)->默认值(0); $table->double('final_weight',8,3)->默认值(0); $table->double('final_cbm',8,3)->默认值(0

我有下表
控制台

Schema::创建('consols',函数(Blueprint$表){
$table->bigIncrements('id');
$table->double('current_weight',8,3)->默认值(0);
$table->double('current_weight',8,3)->默认值(0);
$table->double('final_weight',8,3)->默认值(0);
$table->double('final_cbm',8,3)->默认值(0);
$table->timestamp('finals_sent',0)->nullable();
});
其中包含如下条目:

id |当前|当前| cbm |最终|最终| cbm |最终|发送|
-----------------------------------------------------------------------------------|
1  | 45.000         | 1.000       | 200.000      | 10.000    | 2019-09-26 10:03:59 |
我已在我的型号
Consol
上编写了一个范围来过滤条目,其中:
final\u sent
不为空,
current\u weight>final\u weight
current\u cbm>final\u cbm

当前大于最终($query)的公共函数范围
{
return$query->whereNotNull('finals_sent')->where(函数($query){
$query->where('current\u weight','>','final\u weight')
->或其中(“当前cbm”、“最终cbm”);
});
}
当我使用它并将其转储时-我希望看到结果:

$consols = Consol::ofCurrentGreaterThanFinals()
           ->orderBy('awb', 'DESC')
           ->get()
           ->toArray();
但是,上面的条目仍在显示:

array:1 [▼
  0 => array:24 [▼
    "id" => 1
    "current_weight" => 45.0
    "current_cbm" => 1.0
    "final_weight" => 200.0
    "final_cbm" => 10.0
    "finals_sent" => "2019-09-26 10:03:59"
  ]
]
我不确定我做错了什么?我想我清楚地指出,我只想看到结果,其中:

  • 当前重量>最终重量
  • 当前煤层气>当前重量
  • 发送的最后一个字符不为空
上述条目不符合这一点,因为当前_权重和当前_cbm显然不高于最终_权重和最终_cbm


我做错了什么?

啊,好吧,问题来了。为了比较这两列,必须使用
whereRaw()
作为第二个参数的
where
所需的值,而不是列。 所以这应该是可行的,我用你的数据库和值进行了测试

return $query->whereNotNull('finals_sent')->where(function ($q) {
            $q->whereRaw('current_weight > final_weight')
                ->orWhereRaw('current_cbm > final_cbm');
        });

你的非周范围如何?期末范围似乎很好。我刚更新了问题。如果
current\u weight
current\u cbm
高于它们的
final\u
对应项,我确实打算得到结果。然而,它仍然不应该返回任何结果。@oliverbj更新了我的答案。希望现在能奏效。对不起,有点小误会。