Ober4You's avatar

How to save multiple dropdown/select boxes in DB and model bind them in edit view

I have 3 select boxes in a Form and i want to make model binding but it doesn't work. These are my select boxes in form

    <div class="form-group">
        {!! Form::label('worktype_list1', 'Servicekräfte: ', ['class' => 'col-md-4 control-label']) !!}
        <div class="col-md-2">
        {!! Form::select('worktype_list[]', $worktypes, null, ['id' => 'worktype_list1', 'class' => 'col-md-2 form-control', 'required']) !!}
        </div>
    </div>
    
    <div class="form-group">
        {!! Form::label('worktype_list2', 'Servicekräfte: ', ['class' => 'col-md-4 control-label']) !!}
        <div class="col-md-2">
        {!! Form::select('worktype_list[]', $worktypes, null, ['id' => 'worktype_list2', 'class' => 'col-md-2 form-control', 'required']) !!}
        </div>
    </div>
    
    <div class="form-group">
        {!! Form::label('worktype_list3', 'Servicekräfte: ', ['class' => 'col-md-4 control-label']) !!}
        <div class="col-md-2">
        {!! Form::select('worktype_list[]', $worktypes, null, ['id' => 'worktype_list3', 'class' => 'col-md-2 form-control', 'required']) !!}
        </div>
    </div>

And this is the store method:

    public function store(UrequestsForm $request)
    {

        //dd($request->all());
        $urequest = Auth::user()->requests()->create($request->all());

        $urequest->worktypes()->attach($request->input('worktype_list'));

        return redirect('urequests')->with('success', 'Anfrage erfolgreich gemacht!');
    }

The validation is working fine and saving....but when i go to edit view in every select box is the same and has the same options selected... I have followed this tutorial https://laracasts.com/series/laravel-5-fundamentals/episodes/22 and this works for select box with multiple selects but not for single item select. I think my store method should be adjusted accordingly but i don't know how. Remark i'm using the same form for both create and update view!

If anyone has done this please help.

0 likes
0 replies

Please or to participate in this conversation.