您好我如何实现在数组上插入数据?用口才?现在我在我的
控制器
public function insertSchedule(Request $request)
{
$employAdd = [];
foreach($employAdd as $key){
$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();
}
}
查看
在这里看到我的看法吗?这里的日期是数组,那么当我将其插入数据库时,输出或所插入的数据是最后一个数据,为什么呢?
{!! 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() !!}
答案 0 :(得分:0)
通过我们的聊天,我更新了答案。控制器中的以下内容应该可以工作:
public function insertSchedule(Request $request)
{
Schedule::create([
'employee_no' => $request->input('hidEmployeeno'),
'last_name' => $request->input('hidEmployeeLast'),
'first_name' => $request->input('hidEmployeeFirst'),
'date_today' => $request->input('dateToday'),
'time_in' => $request->input('timeIn'),
'time_out' => $request->input('timeOut'),
]);
}
然后,您需要将表单更新为如下所示,注意Form标记属于您要插入的每一行。
<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>
<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)
{!! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}
<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 ?>'>
<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>
{!! Form::close() !!}
@endforeach
</tbody>
</table>