【发布时间】:2013-10-19 22:58:19
【问题描述】:
我正在使用 Laravel Eloquent 查询构建器,我有一个查询,我希望在多个条件下使用 WHERE 子句。它有效,但并不优雅。
例子:
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
有没有更好的方法来做到这一点,还是我应该坚持这种方法?
【问题讨论】:
-
在如何简化这方面有很多可能性,但这需要一些更实际的代码。你能把代码更新得更真实一点吗?例如,有时多个
->where(...)调用可以替换为->whereIn(...)调用,等等。 -
@Jarek Tkaczyk 的解决方案应该是答案,我同意。但我更喜欢你的代码,比如生成器脚本,用于理解和维护。
-
希望此链接对未来的用户有帮助
-
laravel 要求第一个调用是静态的,但其余的 -> 设计非常糟糕。
-
@paresh-mangukiya 请停止向旧问题添加虚假标签。他们没有帮助。
标签: php laravel eloquent laravel-query-builder