Laravel-将数据数组插入数据库 [英] Laravel - Insert Array of data into database
本文介绍了Laravel-将数据数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我如何实现在数组上插入数据?用口才吗?现在我在我的
Hello how can I achieve to insert data on an array? using eloquent? right now i have this on my
控制器
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();
}
}
查看
在这里看到我的看法吗?这里的日期是数组,那么当我将其插入数据库时,输出或所插入的数据是最后一个数据,为什么呢?
See here my view? the dates here are arrays then when I insert this into the database the output or the data that is being inserted is the last data why is that?
{!! 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() !!}
推荐答案
在我们的聊天中,我更新了答案.控制器中的以下内容应该可以正常工作:
From our chat, I updated the answer. The following in your controller should work:
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标记属于您要插入的每一行.
Then you need to update your form to something like the following, notice the Form tag belongs to each row that you want to insert.
<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>
这篇关于Laravel-将数据数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文