shaNnex
shaNnex

Reputation: 1083

Select option :selected for edit in laravel 5

I am building a page to edit a record, but I am having a difficulty displaying a select option with previously saved/recorded entry as :selected.

The select displays a year option, from 1990 to 2015.

This what i've done so far:

<select name="term_a" class="form-control">
        <option value="0">Choose a year</option>
            @for ($i = 1990; $i < 2015; $i++)
                 <option value="{{ $i }}">{{ $i }}</option>
            @endfor 
</select>

How can make previously stored data as :selected?

EDIT:

I forgot to mention that I did bind my form to a model like this:

{!! Form::model($userdetails) !!}

and this works ok, my only problem is getting the option select fetch the old data as :selected.

Upvotes: 1

Views: 7313

Answers (2)

Samir B
Samir B

Reputation: 152

I advise you to use the HTML/FormBuilder classes it will be easiest

https://github.com/illuminate/html

to use it

// Save your list on a var
$years_list = ['1999', '2000', ... ]

// open form with model parameters and update action
{!! Form::model($model, ['url' => action('MyController@update', $model), 'method' => 'put']) !!}

// in your database the name of column = "year"
{!! Form::select('year', $years_list, null, ['class' => 'form-control']) !!}

// dont forget to close
{!! Form::close() !!}

here is the signature of select on FormBuilder public function select($name, $list = array(), $selected = null, $options = array()){...}

Upvotes: 2

Hkan
Hkan

Reputation: 3383

This should do it:

<option value="{{ $i }}" {{ Input::old('term_a') == $i ? 'selected' : '' }}>{{ $i }}</option>

Upvotes: 2

Related Questions