laravel where clauses on related eloquent model

时间:2017-05-05 20:57:23

标签: laravel eloquent where

嘿,为什么我不能在相关的雄辩模型中使用带有3个参数的where子句?

简单示例(我想要一个< =)

$user->roles->where('active',1);

//正在运作

$user->roles->where('active','=',1);

//无效

我是否只能在3个参数中使用它:

DB::table('users')->where('votes', '=', 100)->get();

而不是:

$xy->users->where('votes', '=', 100);

谢谢,

1 个答案:

答案 0 :(得分:2)

$user->roles正在返回Collection,其上有一个方法where,可以接受两个参数:

$user->roles->where("key", "value");

如文档中所示:https://laravel.com/docs/5.4/collections#method-where

如果您想使用三个,则需要返回Query Builder个实例:

$user->roles()->where("key", "operator", "value")->get();