php - 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"> SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm', 'style'=>"display: inline-block;"])}}</td>
</tr>
@endforeach
</tbody>
</table>
{!! Form::close() !!}
解决方案
问题是您正在使用一种包含多个输入的表单,并且它们没有唯一的名称。如果您检查代码,您的表单将有 31 个名称为timeIn的输入和 31 个名称为timeOut的输入。
当您提交表单时,它会选择最后一个timeIn和timeOut。
两种解决方案
- 制作多个表单(将表单标签放在while循环中,它将在页面中创建31个表单)
- 使用 ajax 向服务器发送数据。
推荐阅读
- google-app-engine - 为什么写入 GCS 存储桶会导致本地开发 blob 存储条目?
- laravel - 只保留特定的 id Laravel
- java - Java Null 取消引用垃圾收集(来自 Fortify 扫描)
- mysql - 在列中搜索 ID 列表并选择不存在的 ID
- graphql - GraphQL 变异结构
- security - 经典的 ASP 应用程序是我们环境的安全漏洞吗?
- video - HTML5 视频标签不支持带有 MPEG4 编解码器的 .mp4 视频文件
- android - 数据在布局中创建并占用空白,但看不到
- python - 熊猫中的前瞻性滚动窗口 - 参差不齐的索引
- http - 如何在 lua 5.0.1 中嵌入 HTTP?