ggoran
ggoran

Reputation: 620

laravel collective form database autoselect

I have forms that will store some data in DB, but next time when I open that page I want autoselected values selected already

{!! Form::select('week_starts', [
                                            'Monday' => 'Monday',
                                             'Tuesday' => 'Tuesday',
                                             'Wednesday' => 'Wednesday',
                                             'Thursday' => 'Thursday',
                                             'Friday' => 'Friday',
                                             'Saturday' => 'Saturday',
                                             'Sunday' => 'Sunday'
                                            ]) !!}

so I need form that will check from DB (that's not problem) and if found in database example Monday and Tuesday these fields will be autoselected with param selected=true

any idea how to do that. I tried to <?php if... ?> but its not working inside {!! ... !!}

Upvotes: 2

Views: 203

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163848

You should pass and ID of selected option as third parameter:

{!! Form::select('week_starts', [
    'Monday' => 'Monday',
    Tuesday' => 'Tuesday',
    Wednesday' => 'Wednesday',
    Thursday' => 'Thursday',
    Friday' => 'Friday',
    Saturday' => 'Saturday',
    Sunday' => 'Sunday'
], 'Friday') !!}

You should get this data from DB in controller or a model and pass as variable into the view.

Or you could use Form::model binding, in this case ID will be set automatically.

Upvotes: 1

Related Questions