laravel Pluck with Where 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43570676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pluck with Where condition
提问by siddiq
I can take the list using
我可以使用列表
$specialities = Speciality::pluck('name','id')
Why isn't the following code working? What could be an alternative?
I am returning this array by ajax to form a select box. So I thought pluck
(list in laravel 4+) would be the right choice.
为什么下面的代码不起作用?什么是替代方案?我通过ajax返回这个数组以形成一个选择框。所以我认为pluck
(在 laravel 4+ 中列出)将是正确的选择。
$specialities = Speciality::pluck('name','id')->where('role_id',$request->roleid);
回答by siddiq
I found the mistake. I should use pluck with where condition like below.
我发现了错误。我应该在如下条件下使用 pluck。
$specialities = Speciality::where('role_id',$request->roleid)->pluck('name','id');
Pluck won't filter anything, but it gives only what needed. So filtering has to be done before that.
Pluck 不会过滤任何东西,但它只提供需要的东西。所以在此之前必须进行过滤。