Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pluck with Where condition

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);
like image 423
siddiq Avatar asked Apr 23 '17 11:04

siddiq


People also ask

Where do we use pluck?

The pluck helper method is used to retrieve a list of specific values from a given $array. It is most useful when used against arrays of objects, but will also work with arrays just as well. You might often run into a situation where you have to extract certain value from a collection.

What is pluck method?

log() method returns the natural logarithm (base e) of a double value as a parameter. There are various cases : If the argument is NaN or less than zero, then the result is NaN. If the argument is positive infinity, then the result is positive infinity.

What is Pluck () in laravel?

Laravel Pluck() is a Laravel Collections method used to extract certain values from the collection. You might often would want to extract certain data from the collection i.e Eloquent collection.


1 Answers

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.

like image 123
siddiq Avatar answered Oct 04 '22 04:10

siddiq