Laravel eloquent 查询组where子句 returns 错误查询

Laravel eloquent query group where clause returns wrong query

我有一个 MySQL 查询:

select * from `contents`
 where `vei_sn` = '22566' 
    and `op_date` >= '2022-02-07' 
    and `op_date` <= '2022-02-07'
     and not (`ecu` = 'ACM' and `ecu_sub` = 2.1 and `parameters` = 'TorqueLimit2') 
order by `op_full_date` desc limit 31 offset 0

我想把它变成eloquent。 我写了这个 eloquent 查询,但它输出了错误的查询结果:

OplogContent::query()
            ->where('vei_sn', $device->serial_number)
            ->where('op_date', '>=', $request->date_from)
            ->where('op_date', '<=', $request->date_to)
            ->where(function ($q) {
                $q->where('ecu', '!=', 'ACM')
                  ->where('ecu_sub', '!=', 2.1)
                  ->where('parameters', '!=', 'TorqueLimit2');
            });
            ->orderBy('op_full_date', 'desc')
            ->simplePaginate(30)

//// but it returns this MySQL query //////
select * from `contents` 
 where `vei_sn` = '22566' 
  and `op_date` >= '2022-02-07' 
   and `op_date` <= '2022-02-07' 
    and (`ecu` != 'ACM' and `ecu_sub` != 2.1 and `parameters` != 'TorqueLimit2') 
order by `op_full_date` desc limit 31 offset 0

如何更改我的 eloquent 查询,MySQL where 分组子句将是 and not (`ecu` = 'ACM' and `ecu_sub` = 2.1 and `parameters` = 'TorqueLimit2')

问题是关于布尔代数的。如果您使用 NOT(a & b) 它等于 不是 |不是 b.

所以这一行翻译成查询:

and not ('ecu' = 'ACM' and 'ecu_sub' = 2.1 and 'parameters' = 'TorqueLimit2')

 ->where(function ($q) {
            $q->where('ecu', '!=', 'ACM')
              ->orWhere('ecu_sub', '!=', 2.1)
              ->orWhere('parameters', '!=', 'TorqueLimit2');
        });