首页 > 解决方案 > Laravel 仅在最后一个列表或数据中插入数据

问题描述

我要插入数据具体看我的页面。

在此处输入图像描述

当我设置时间时,数据将被插入。但是我现在拥有的是插入的数据是该表上的最后一个数据,我该January-31-2019如何修复它,所以当我插入数据时,数据将插入表上列出的任何数据。

控制器

public function insertSchedule(Request $request)
{
    $employeeTimeSet = new Schedule;
    $employeeTimeSet->employee_no = $request->input('hidEmployeeno');
    $employeeTimeSet->last_name = $request->input('hidEmployeeLast');
    $employeeTimeSet->first_name = $request->input('hidEmployeeFirst');
    $employeeTimeSet->date_today = $request->input('dateToday');
    $employeeTimeSet->time_in = $request->input('timeIn');
    $employeeTimeSet->time_out = $request->input('timeOut');
    $employeeTimeSet->save();

    $notification = array(
        'message' => 'Click PLAN TIME! Employee Time Set!',
        'alert-type' => 'success'
    );

    return redirect()->back()->with($notification, 'Employee Time Set');
}

看法

{!! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}
<div class="row">
    <div class="form-group col-md-12">
        <small>Employee No. and Name:</small>
        <b><i> {{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b>
        <input type="hidden" name="hidEmployeeno" value='<?php echo $employee->employee_no ?>'>
        <input type="hidden" name="hidEmployeeLast" value='<?php echo $employee->last_name ?>'>
        <input type="hidden" name="hidEmployeeFirst" value='<?php echo $employee->first_name ?>'>
        <hr>
    </div>
</div>
@php
    $today = today(); 
    $dates = []; 

    for($i=1; $i < $today->daysInMonth + 1; ++$i) {
        $dates[] = \Carbon\Carbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');
    }
@endphp

<table class="table">
    <thead>
    <tr>
        <th>DATE TODAY</th>
        <th>TIME IN</th>
        <th>TIME OUT</th>
        <th>ACTION</th>
    </tr>
    </thead>
    <tbody>
    @foreach($dates as $date)
        <tr>
            <td><b>{{ $date }}</b></td>
            <input type="hidden" name="dateToday" value="{{ $date }}">
            <td><input type="time" name="timeIn" class="form-control col-md-10"></td>
            <td><input type="time" name="timeOut" class="form-control col-md-10"></td>
            <td> {{Form::button('<i class="fa fa-clock">&nbsp;&nbsp;SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm',  'style'=>"display: inline-block;"])}}</td>
        </tr>
    @endforeach
    </tbody>
</table>
{!! Form::close() !!}

标签: phparrayslaravellaravel-5eloquent

解决方案


问题是您正在使用一种包含多个输入的表单,并且它们没有唯一的名称。如果您检查代码,您的表单将有 31 个名称为timeIn的输入和 31 个名称为timeOut的输入。

当您提交表单时,它会选择最后一个timeIntimeOut

两种解决方案

  1. 制作多个表单(将表单标签放在while循环中,它将在页面中创建31个表单)
  2. 使用 ajax 向服务器发送数据。

推荐阅读