laravel表单数组多次插入

时间:2019-07-16 13:41:38

标签: vue.js laravel-5

我正在使用laravel新手,但遇到以下问题。 我正在使用laravel 5.8,想一次将表单数组提交到同一张表。我有一个名为时间表循环通过日期数组的形式。 id希望收集每个日期的小时数,并同时保存它们,如下图所示。 https://imgur.com/gNdJVNE

下面是我的表单和控制器

TimesheetController.php

public function store(Request $request, Timesheet $timesheet, Analysis $analysis, Activity $activity)
{
    $validatedData = $request->validate([

        'date_id' => 'required',
        'analysis_id' => 'required',
        'activity_id' => 'max:1000',
        'hour' => 'required|max:100',
        'activity_id1' => 'max:1000',
        'hour1' => 'max:100',
        'activity_id2' => 'max:1000',
        'hour2' => 'max:100',
        'activity_id3' => 'max:1000',
        'hour3' => 'max:100',
        'activity_id4' => 'max:1000',
        'hour4' => 'max:100',
        'activity_id5' => 'max:1000',
        'hour5' => 'max:100',
        'total_hours' => 'required|max:100',
        'approved' => 'required',
        'approved_by' => 'required'



    ]);



    $input = Timesheet::all();
    $insert=array();
         foreach ($input['date_id'] as $key => $date_id) {
            $insert[$key]['date_id'] = $date_id;
        }
        foreach ($input['analysis_id'] as $key => $analysis_id) {
            $insert[$key]['analysis_id'] = $analysis_id;
        }
        foreach ($input['activity_id'] as $key => $activity_id) {
            $insert[$key]['activity_id'] = $activity_id;
        }
        foreach ($input['hour'] as $key => $hour) {
            $insert[$key]['hour'] = $hour;
        }
        foreach ($input['activity_id1'] as $key => $activity_id1) {
            $insert[$key]['activity_id1'] = $activity_id1;
        }
        foreach ($input['hour1'] as $key => $hour1)  {
            $insert[$key]['hour1'] = $hour1;
        }
        foreach ($input['activity_id2'] as $key => $activity_id2) {
            $insert[$key]['activity_id2'] = $activity_id2;
        }
        foreach ($input['hour2'] as $key => $hour2)  {
            $insert[$key]['hour2'] = $hour2;
        }
        foreach ($input['activity_id3'] as $key => $activity_id3) {
            $insert[$key]['activity_id3'] = $activity_id3;
        }
        foreach ($input['hour3'] as $key => $hour3)  {
            $insert[$key]['hour3'] = $hour3;
        }
        foreach ($input['activity_id4'] as $key => $activity_id4) {
            $insert[$key]['activity_id4'] = $activity_id4;
        }
        foreach ($input['hour4'] as $key => $hour4)  {
            $insert[$key]['hour4'] = $hour4;
        }
        foreach ($input['activity_id5'] as $key => $activity_id5) {
            $insert[$key]['activity_id5'] = $activity_id5;
        }
        foreach ($input['hour5'] as $key => $hour5)  {
            $insert[$key]['hour5'] = $hour5;
        }
        foreach ($input['total_hours'] as $key => $total_hours) {
            $insert[$key]['total_hours'] = $total_hours;
        }
        foreach ($input['approved'] as $key => $approved) {
            $insert[$key]['approved'] = $approved;
        }
        foreach ($input['approved_by'] as $key => $approved_by) {
            $insert[$key]['approved_by'] = $approved_by;
        }

Timesheet::insert($insert);

    return redirect()->route('timesheets.index')->with('success', "Activity Created ");
}

<form action="{{route('timesheets.store')}}" method="post" id="create_analysis">
     @csrf
@foreach($dates as $date)
<div class="row border" style="padding:3px;" id="timesheet">

    <strong>{{$date->date}}</strong>
    <input type="hidden" id="date_id" name="date_id[]" value="{{$date->id}}">
    <input type="hidden" id="analysis_id" name="analysis_id[]" value="{{$analysis->analysisid}}">

    <div class="col-md-1">
            <a href="#" data-toggle="tooltip" title="{{@$tact1->projectname}}">
                {{@$tact1->projectcode}}
            </a>
            <input type="hidden" id="activity_id" name="activity_id[]" value="{{@$tact1->activity_id}}">
        <div class="input-group mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
            </div>
            <input type="text" name="hour[]" value="0"  id="hour" v-model.number="hour" class="form-control {{ $errors->has('hour') ? 'is-invalid': ''}}">
        </div>
        @if ($errors->has('hour'))
        <div class="invalid-feedback">
            <strong>
                {{ $errors->first('hour') }}
            </strong>
        </div>
            @endif
    </div>

    @if(empty($analysis->act1))

    @else
        <div class="row col-md-1">
                <a href="#" data-toggle="tooltip" title="{{@$tact2->projectname1}}">
                     {{@$tact2->projectcode1}}
                </a>
                <input type="hidden" id="activity_id1" name="activity_id1[]" value="{{@$tact2->activity_id1}}">

                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
                        </div>
                    <input type="text" name="hour1[]" value="0"  id="hour1" v-model.number="hour1" class="form-control {{ $errors->has('hour1') ? 'is-invalid': ''}}">
                        @if ($errors->has('hour1'))
                        <div class="invalid-feedback">
                            <strong>
                                {{ $errors->first('hour1') }}
                            </strong>
                        </div>
                        @endif

            </div>
        </div>&nbsp;&nbsp;
    @endif

    @if(empty($analysis->act2))

    @else
        <div class="row col-md-1">
                <a href="#" data-toggle="tooltip" title="{{@$tact3->projectname2}}">
                     {{@$tact3->projectcode2}}
                </a>
                <input type="hidden" id="activity_id2" name="activity_id2[]" value="{{@$tact3->activity_id2}}">
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
                </div>
                <input type="text" name="hour2[]" value="0"  id="hour2" v-model.number="hour2" class="form-control {{ $errors->has('hour2') ? 'is-invalid': ''}}">
                @if ($errors->has('hour2'))
                <div class="invalid-feedback">
                    <strong>
                        {{ $errors->first('hour2') }}
                    </strong>
                </div>
                    @endif
            </div>
        </div>&nbsp;&nbsp;
    @endif
    @if(empty($analysis->act3))

    @else
        <div class="row col-md-1">
            <a href="#" data-toggle="tooltip" title="{{@$tact4->projectname3}}">
                {{@$tact4->projectcode3}}
            </a>
            <input type="hidden" id="activity_id3" name="activity_id3[]" value="{{@$tact4->activity_id3}}">
                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
                    </div>
                        <input type="text" name="hour3[]" value="0"  id="hour3" v-model.number="hour3" class="form-control {{ $errors->has('hour3') ? 'is-invalid': ''}}">
                        @if ($errors->has('hour3'))
                        <div class="invalid-feedback">
                            <strong>
                                {{ $errors->first('hour3') }}
                            </strong>
                        </div>
                        @endif
                </div>

        </div>&nbsp;&nbsp;
    @endif
    @if(empty($analysis->act4))

    @else
        <div class="row col-md-1">
            <a href="#" data-toggle="tooltip" title="{{@$tact4->projectname4}}">
                {{@$tact5->projectcode4}}
            </a>
            <input type="hidden" id="activity_id4" name="activity_id4[]" value="{{@$tact5->activity_id4}}">

                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
                    </div>
                    <input type="text" name="hour4[]" value="0"  id="hour4" v-model.number="hour4" class="form-control {{ $errors->has('hour4') ? 'is-invalid': ''}}">
                    @if ($errors->has('hour4'))
                    <div class="invalid-feedback">
                        <strong>
                            {{ $errors->first('hour4') }}
                        </strong>
                    </div>
                    @endif
                </div>

        </div>&nbsp;&nbsp;
    @endif
    @if(empty($analysis->act5))

    @else
    <div class="row col-md-1">
            <a href="#" data-toggle="tooltip" title="{{@$tact4->projectname5}}">
                {{@$tact6->projectcode5}}
            </a>
            <input type="hidden" id="activity_id5" name="activity_id5[]" value="{{@$tact6->activity_id5}}">
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text"><i class="fa fa-clock-o"></i></span>
                </div>
                <input type="text" name="hour5[]" value="0"  id="hour5" v-model.number="hour5" class="form-control {{ $errors->has('hour5') ? 'is-invalid': ''}}">
                @if ($errors->has('hour5'))
                <div class="invalid-feedback">
                    <strong>
                        {{ $errors->first('hour5') }}
                    </strong>
                </div>
                @endif
            </div>

    </div>

    @endif

        <div class="col-md-3">
           Total Hours

                <div class="input-group mb-3">
                        <div class="input-group-prepend">
                                <span class="input-group-text"><i class="fa fa-list-ol"></i></span>
                            </div>
                    <input type="text" name="total_hours[]" :value="TotalTimesheets"  id="total_hours" class="form-control {{ $errors->has('total_percentage') ? 'is-invalid': ''}}" max="9" >
                    @if ($errors->has('total_hours'))
                    <div class="invalid-feedback">
                        <strong>
                            {{ $errors->first('total_hours') }}
                        </strong>
                    </div>
                    @endif
                </div>
        </div>




<input type="hidden" id="approved" name="approved[]" value="No">
<input type="hidden" id="approved_by" name="approved_by[]" value="{{$approved->supervisor_id}}">
</div>
@endforeach
<br />
<br />
        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-lg">{{ $buttonText }}</button>
        </div>



</form>

0 个答案:

没有答案