Request returns last row only.

Posted 2 months ago by joshblevins

I have a form where the user can add rows to add multiple rows of data at one time.

When I dump the array, I get the last row only. I can't seem to see where the flaw is. The controller works and inserts data however only the last row.

@extends('layouts.reports')

@section('page-title', trans('Human Resources'))
@section('page-heading', trans('Human Resources'))

@section('breadcrumbs')
<li class="breadcrumb-item active">
    @lang('Human Resources')
</li>
@stop

@section('content')

@include('partials.toastr')
<div class="row">
    <form action="{{ route('application.store_schoolhx') }}" method="post"  class="education_form">

        <table id="school_table" align=center class="table table">
            <tr>
                <th>Please enter all EMS state certifications.</th>
            </tr>
            <tr id="row1">
                <input type='hidden' class='form-control' name='edu[index][application_id][]' value="{{$employee}}" placeholder='Start Date'/>
                <td> <div class ="md-form"><input type='text' class='form-control' name='edu[index][completed][]' placeholder='Year Completed'></div> </td>
                <td> <div class ="md-form"><input type='text' class='form-control' name='edu[index][school][]' placeholder='School Name'></div> </td>
                <td> <div class ="md-form"><input type='text' class='form-control' name='edu[index][state][]' placeholder='State'></div> </td>
                <td> <div class ="md-form"><input type='text' class='form-control' name='edu[index][degree][]' placeholder='Degree/Certification'></div> </td>
            </tr>
        </table>
        <input type="button" class="btn btn-primary" onclick="add_row_school();" value="ADD ROW">

        <button class="btn btn-primary btn-block my-4" type="submit" >Add Education History</button>

        @csrf
        {!! Form::close() !!}
</div>


@stop

@section('styles')


@stop

@section('scripts')
<script>
    // Data Picker Initialization
    $('.datepicker').pickadate();
</script>


<script type="text/javascript">
    function add_row_school() {
        $rowno = $("#school_table tr").length;
        $rowno = $rowno + 1;
        $("#school_table tr:last").after("" +
            "<tr id='row" + $rowno + "'>" +
            "<input type='hidden' class='form-control' name='edu[index][application_id][]' value='{{$employee}}' placeholder='Start Date'>"+
            "<td> <div class =\"md-form\"><input type='text' class='form-control' name='edu[index][completed][]' placeholder='Year Completed'></div> </td>" +
            "<td> <div class =\"md-form\"><input type='text' class='form-control' name='edu[index][school][]' placeholder='School Name'></div> </td>" +
            "<td> <div class =\"md-form\"><input type='text' class='form-control' name='edu[index][state][]' placeholder='State'></div> </td>" +
            "<td> <div class =\"md-form\"><input type='text' class='form-control' name='edu[index][degree][]' placeholder='Degree/Certification'></div> </td>" +
            "<td><input type='button' class='btn btn-danger' value='DELETE' onclick=delete_row('row" + $rowno + "')></div></td>" +
            "</tr>");
    }

    function delete_row(rowno) {
        $('#' + rowno).remove();
    }
</script>


@stop



Route

Route::post('application/store/schoolhx', '[email protected]_schoolhx')->name('application.store_schoolhx');

Controller

public function store_schoolhx(Request $request)
    {
        dd($request->edu);
        
        foreach($request->edu as $row)
        {
            ApplicationEducation::create($row);
            
        }
        
        return view('hr.appcomplete');
  
    }

Any help appreciated, thanks in advanced.

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

Reply to

Use Markdown with GitHub-flavored code blocks.