Search code examples
phplaravellaravel-4mutators

Use accessor mutator's within Laravel 4 lists method


I have a simple mutator:

public function getFullnameAttribute($value)
{
    return $this->first_name. ' ' .$this->last_name;
}

But I also have a method that returns a list of specific users for use in a select input:

static function getDeliveryManagers()
{
    return User::IsDeliveryManager()->lists('first_name', 'id');
}

I need that array returned to be ['First Name Last Name', 'id']

But the mutator doesn't work, e.g.

return User::IsDeliveryManager()->lists('Fullname', 'id');

I found this: https://github.com/laravel/framework/issues/668

Which Taylor marks as being done but I cant get it working. Using Laravel 4.2.

Any help?


Solution

  • You need to call the lists() on Eloquent Collection. So make sure IsDeliveryManager() is returning correct values. Its better if you can share the code of IsDeliveryManager(). If its is query scope you need to call get() before lists().

    return User::IsDeliveryManager()->get()->lists('fullname', 'id');