【问题标题】:How can i query build in laravel我如何在 laravel 中查询构建
【发布时间】:2022-06-27 23:22:18
【问题描述】:

如果我想在 sql 中做这样的事情: 选择 * 从表 哪里 id = 1111 AND(column1 为 null 或 column2 = 'sample')

我尝试使用“->where(..)->orWhereNull(...)”,但这不是我的意思(就像没有 () 是查询)

【问题讨论】:

  • 阅读 Laravel 中的逻辑分组 - laravel.com/docs/9.x/queries#logical-grouping
  • 那么这里有什么问题呢?我不明白。
  • 如果我尝试使用 "->where(..)->orWhereNull(...)" 我得到所有 id 等于 222,333,444 的表达式...

标签: php jquery laravel


【解决方案1】:

在 Laravel 中使用逻辑分组,感谢 aynber:

->where('id', 1111)
->where(function ($q) {
            $q->where('column2', 'sample')
            ->orWhereNull('column1');
        })

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2018-04-22
    • 2015-10-09
    • 2020-04-20
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多