1

How to add "selected" on the dropdown option in Laravel?

I am doing editing profile. In the profile I have a dropdown list, the options are pulled from database, like this.

//Controller

$gender= Gender::lists('gender', 'id');

//View

{{ Form::select('gender', $gender, null, array('class' => 'form-control')) }}

//Output

<select class="form-control" name="gender"><option value="1">Male</option><option value="2">Female</option></select>

My question: If the user is female, how to add "selected" in the female option if the user has saved female in database before, so that I can show the gender when the page is loaded?

1 Answer 1

4

You can set the default value at the 3rd parameter.

Assume Female id is 2

{{ Form::select('gender', $gender, 2, array('class' => 'form-control')) }}

If you already has a $user with sex_id attribute(or your sex column), You can pass it to the Form::select as well.

{{ Form::select('gender', $gender, $user->sex_id, array('class' => 'form-control')) }}

http://laravel.com/docs/4.2/html#drop-down-lists

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.