【问题标题】:How can I put '!=' with an array in where in Laravel Query builder?如何在 Laravel Query builder 中将 '!=' 与数组放在一起?
【发布时间】:2022-06-30 00:02:28
【问题描述】:

如何在 laravel 查询生成器的 where 中写不相等?

我想这样查询 例如

select * from Table where a != '1' and a != '2' and a != '4' and a != '6' ;

$removeIdListArray = (1,2,4,6);

$removedIdList =  Stack::
            ->where('columnA',$removeIdListArray);

//↑What should I do?

【问题讨论】:

  • 您有语法错误:$removeIdListArray = (1,2,4,6); 应该是 $removeIdListArray = array(1,2,4,6);$removeIdListArray = [1,2,4,6];

标签: laravel


【解决方案1】:

你可以使用whereNotIn并传递一个数组作为第二个参数

$removeIdListArray = [1,2,4,6];

$removedIdList =  Stack::whereNotIn('columnA', $removeIdListArray);

参考:Database: Query Builder

【讨论】:

    【解决方案2】:

    简单地说:

    ->where('columnA', '!=', $removeIdListArray);
    

    ->whereNot('columnA', $removeIdListArray);
    

    【讨论】:

    • where()whereNot() 用于单个值,而不是多个值。
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2019-08-14
    • 2020-07-26
    • 2020-04-14
    • 2021-12-15
    相关资源
    最近更新 更多