2017-05-05 52 views
0

嘿,爲什麼我不能使用有關雄辯模型的3個參數的where子句?laravel在相關的雄辯模型上的條款

簡單的例子(我希望它爲一個< =)

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

//正在

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

//不工作

我可以用3個PARAMS只用它在:

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

而不是:

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

謝謝,

回答

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(); 
+0

非常感謝你 – user7970909