Laravel 4 Fill a dropdown list with two database columns?

echo Form::select(
    'campaign_user_id',
    User::find(1)->profile->lists('first_name', 'id'),
    Input::get('campaign_user_id', $campaign->user_id),
    array(
      "placeholder" => "US",
      'class' => 'form-control',
      'disabled' => 'disabled'
    )
)

The above code is used to create a dropdown select list. It retrieves the list of users from the database. In line 3 you can seeUser::find(1)->profile->lists('first_name', 'id')

It gets the first_name column, I need to somehow get the columns of the first and last name and combine them in this list. Thus, the list value is still the user ID and the full name is displayed.

Any idea how to make this work with 2 DB columns first_nameand last_name? Or another way to reach my ultimate goal?

+4
source share
1 answer

// In your model

class User extends Eloquent
{
    public function getFullNameAttribute()
    {
        return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
    }
}

then take it like this:

User::find(1)->profile->lists('full_name', 'id');

OR

User::find(1)->profile->select(DB::raw('concat (first_name," ",last_name) as full_name,id'))->lists('full_name', 'id');

:)

+6

Source: https://habr.com/ru/post/1527222/


All Articles