对数据库中的一列使用多个条件进行查询,laravel
我需要获取级别从一个值到另一个值的用户。好吧,我不知道如何进行正确的查询。这是我的错误尝试:
$thread = Threads::findOrFail($id);
$users= Users::where(
['rank', '>', $thread->rank_from],
['rank', '<', $thread->rank_to]
)->get();
我的错误是:
未找到列:1054 'where 子句' 中的未知列 '0'(SQL:select * from accounts where (0 = rank and 1 = > and 2 = 21))
谢谢!
回答
将其更改为这种格式的括号。但在这里,因为它是一个 AND 条件,所以你真的不需要括号,除非你没有向我们展示整个请求并且你有一些 orWhere ......
$users = Users::where(function ($query) use ($thread) {
$query->where('level', '>', $thread->level_from)
->where('level', '<', $thread->level_to);
})->get();
这是在文档Eloquent 查询中
如果您发布的查询是整个查询,只需堆叠wheres
$users= Users::where('rank', '>', $thread->rank_from)
->where('rank', '<', $thread->rank_to)
->get();