33

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.

$specialities = Speciality::pluck('name','id')->where('role_id',$request->roleid);
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
siddiq
  • 1,693
  • 3
  • 17
  • 44
  • Is [tag:pluck] the right tag for this question? When I hover over it, it says that pluck is a Rails function. – Cave Johnson Apr 26 '17 at 06:47
  • Oh yes. Just now i am also noticing it. I think the pluck has the same meaning in both rail and laravel. But there is no separate i can find for laravel pluck. What should i do? – siddiq Apr 26 '17 at 07:28
  • I'm not sure myself. Just trying to probe for answers from people. I can't find a pluck for laravel. Since you say that it is the same in rail and laravel, maybe you can just keep it. – Cave Johnson Apr 26 '17 at 07:31
  • `$specialities = Speciality::where('role_id',$request->roleid)->get()->pluck('name','id');`? – Alex Harris May 10 '17 at 21:44

1 Answers1

74

I found the mistake. I should use pluck with where condition like below.

$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.

siddiq
  • 1,693
  • 3
  • 17
  • 44