Form model binding relations - How to bind/populate relations in a form?

Posted 3 years ago by RuRu

I have a form extracted in a partial which is dealing with three different models. Employee, Address and Skills. I can save all three and thats fine. The issue is editing.

I model bind the employee on my edit page and it populates only the employee fields, leaving the rest blank.

{!! Form::model($employee, ['method' => 'PATCH', 'action' => ['EmployeeController@update', $employee->id]]) !!}

I have to do custom logic to get the skills bound. It works fine within edit and create page. But to bind Address I have to do it manually, and it breaks my create page. Where the goal is to reuse the forms partials.

Simple solutions is to make few more partials and deal it manually. But there has to be a way to automate this, which would prevent boiler plate code which we all dont want to write.

So what am I missing and how can I do what I am attempting to do?

Here is what I have


    <legend>Employee Details</legend>
    <div class="col-md-6">
        <div class="form-group">
            {!! Form::label('first_name', 'First Name:', ['class' => 'col-md-3 control-label']) !!}
            {!! Form::text('first_name', null, ['class' => 'form-control']) !!}

        <!-- Last Name Form Input -->
        <div class="form-group">
            {!! Form::label('last_name', 'Last Name:', ['class' => 'col-md-3 control-label']) !!}
            {!! Form::text('last_name', null, ['class' => 'form-control']) !!}

        <div class="col-md-6 col-md-offset-2">

            <label>Select your competences</label>

            <div class="form-group">
                @foreach($skills as $skill)
                    {!! Form::label('skill', $skill->skill, ['class' => 'col-md-10 control-label']) !!}
                        {!! Form::checkbox('skill[]', $skill->id, $employee->hasSkill($skill->id)) !!}
                        {!! Form::checkbox('skill[]', $skill->id) !!}

                    {{--<input type="checkbox" name="skill[]" id="{{$skill->id}}" value="{{$skill->id}}" @if($employee->hasSkill($skill->id) checked @endif>--}}

    <legend>Address Details</legend>
        <div class="col-md-6">
            <!-- Street Form Input -->
            <div class="form-group">
                {!! Form::label('street', 'Street:', ['class' => 'col-md-3 control-label']) !!}
                {!! Form::text('street', null, ['class' => 'form-control']) !!}

            <!-- House Number or Name Form Input -->
            <div class="form-group">
                {!! Form::label('house_id', 'House Number or Name:', ['class' => 'col-md-3 control-label']) !!}
                {!! Form::text('house_id', null, ['class' => 'form-control']) !!}

            <!-- City Form Input -->
            <div class="form-group">
                {!! Form::label('city', 'City:', ['class' => 'col-md-3 control-label']) !!}
                {!! Form::text('city', null, ['class' => 'form-control']) !!}

            <!-- Post code Form Input -->
            <div class="form-group">
                {!! Form::label('post_code', 'Post code:', ['class' => 'col-md-3 control-label']) !!}
                {!! Form::text('post_code', null, ['class' => 'form-control']) !!}


            <div class="form-group">
                {!! Form::submit($submitButton, ['class' => 'btn btn-primary form-control']) !!}


And my controller

  public function edit(Employee $employee)
        $skills = Skill::all();
        return view('employee.edit', compact('employee', 'skills'));

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.