I ran into a problem, and this is really a minor inconvenience, but ...
Essentially, I need an associative array for the select box. This is usually achieved using the pluck() function.
The problem is that the attribute that I want to use as "text" does not actually exist in the database, it is a mutator that combines two fields into one.
public function getNameAttribute() { return $this->first_name . ' ' . $this->last_name; }
I know that adding the 'name' field to the $appends in the model will include this field when casting the model to an array, however, this doesn't seem to work with pluck()
Is there an easy way to achieve what you want? Function or declaration I'm missing? Anything more eloquent than looping through a collection manually and creating your own associated array?
Refresh
I am an idiot. I passed an array to collect instead of two parameters. Obviously pluck uses the $appends attribute. Please note that this only works when working with collections:
$collection->pluck('mutatedValue', 'key');
NOT query builder
$queryBuilder->pluck('mutatedValue', 'id');
source share