Richie
Richie

Reputation: 1439

Laravel 4.2 Dropdown List

I want to populate a select input using data stored in my database.

I have firstname and lastname fields for my clients and I want to display fullname (both firstname and lastname) on the dropdown list.

I'm using Client::lists('firstname', 'id') to generate the list. Apparently this will only show the firstname only.

How do I combine the two fields in the above code, or is there a better alternative to this?

Another related question, how do I set the first option to be --please select a client-- instead of getting the first client straight away on top.

Upvotes: 1

Views: 872

Answers (1)

terry low
terry low

Reputation: 10628

you define your accessor by adding the following code to your user model

public function getUserNameAttribute()
{
    return "{$this->lastname} {$this->firstname}";
}

and query your model this way

$list = User::get(['firstname','lastname','id'])->lists('username','id');

regard the --please select a client-- ,

you may do a array merge or do a plus like so

Form::select('list',['0'=>'--please select a client--'] + $list);

Upvotes: 1

Related Questions