2

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 {!! ... !!}

ggoran
  • 630
  • 2
  • 12
  • 29

1 Answers1

1

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • nice, this will work for dropdown, any idea how to pass true/false on checkbox? – ggoran Nov 05 '16 at 17:23
  • For checkbox you [should pass `true` as third parameter](https://laravelcollective.com/docs/5.2/html#checkboxes-and-radio-buttons) to make the checkbox to be checked. – Alexey Mezenin Nov 05 '16 at 17:26