Custom field in Eloquent's lists method

Submitted by JuJu57 - 10 years ago

Sometimes you'll need to put custom fields in your dropdown field, like user's firstname and name. That's how you can do it.

// In your Model file add the following method
// Assuming that your model "firstname" and "name" are existing in your model
public function getFullNameAttribute()
{
    return "$this->firstname $this->name";
}

// In your controller file
$users = User::lists('full_name', 'id');

// In your view 
{{ Form::select('users', $users) }}