【问题标题】:How can I do a where AND where in Laravel Eloquent?我如何在 Laravel Eloquent 中执行 where 和 where ?
【发布时间】:2015-10-02 11:06:12
【问题描述】:

这是我目前拥有的:

$query->orderBy('id', 'DESC')

        ->where('totalColorless', '!=', 0) // currently removes all of these...
        ->where('totalResidual', '!=', 0) // ...and all of these

        ->get();

我怎样才能使它删除同时满足两个 WHERE 条件的行?

【问题讨论】:

  • 或者我误解了您的问题,或者您的查询已经按照您的要求进行。这是在做WHERE private != 1 AND totalColorless != 0 AND totalResidual != 0
  • 有些列的 totalColorless = 0 但 totalResidual = 1,反之亦然。基本上我想同时删除 totalColorless = 0 和 totalResidual = 0 的行。
  • Err 那么就这样做吧.. 为什么你的查询不能像这样->where('totalColorless', '=', 0)->where('totalResidual', '=', 0)
  • ->where('totalColorless', 0)->where('totalResidual', 0) 应该可以工作。

标签: php laravel eloquent fluent


【解决方案1】:

试试“orWhere”:

[...]
->where('totalColorless', '!=', 0)
->orWhere('totalResidual', '!=', 0)
[...]

【讨论】:

  • 我尝试了 orWhere,奇怪的是它工作正常。如果你改成 orWhere 我会选择你的作为答案。
  • 改变了,但你真的需要orWhere吗?
  • 奇怪的是,orWhere 的作用与 php 中的 AND 相同。也许 MySQL 命令的命名很奇怪?
  • 我认为您可以使用->getQueryLog()->toSql() 获取查询字符串并检查它
【解决方案2】:

我认为您必须将orderBy() 放在get() 之前和where() 之后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2018-02-21
    相关资源
    最近更新 更多