javascript - 使用 Laravel 将多个数据插入数据库
问题描述
我正在尝试使用数组和 javascript 将多个数据插入到我的数据库中。但是发生的事情是,我只能将一个数据提交到我的投诉表中。未插入平衡器。没有出现错误。
用户表
id
role
email
typable_id
typable_type
买家表
id
name
buyer_id
address
phone_no
缺陷表
id
name
投诉表
id
defect_id
image
description
report_by
投诉控制器.php
class ComplaintController extends Controller
{
public function index()
{
return view('buyers.complaint');
}
public function create(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $request->image[$item],
'description' => $request->description[$item],
'report_by' => auth()->user()->typable->buyer_id
);
Complaint::insert($data);
}
}
return redirect('/report-form')->with('success','Your report is submitted!');
}
投诉刀片.php
<div class="panel-heading">
<h3 class="panel-title"><strong>Make New Report</strong></h3>
</div>
<div class="panel-body">
<div>
<div class="panel">
<table class="table table-bordered">
<thead>
<tr>
<th><center>Type of Defect</center></th>
<th><center>Image</center></th>
<th><center>Description</center></th>
<th><center>Action</center></th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%">
<form action="/report-create" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<select class="form-control" name="defect_id[]">
<option value="" selected>Choose Defect</option>
@foreach(App\Defect::all() as $defect)
<option value="{{$defect->id}}">{{$defect->name}}</option>
@endforeach
</form>
</td>
<td width="15%">
<input type="file" class="form-control-file" name="image[]">
</td>
<td width="45%">
<input type="text" class="form-control" name="description[]">
</td>
<td width="10%">
<button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<center><button type="submit" class="btn btn-primary">Submit</button></center>
</div>
刀片中的 JavaScript
<script>
$(document).ready(function () {
$('#add-btn').on('click',function () {
var html = '';
html += '<tr>';
html += '<td><select class="form-control" name="defect_id[]"><option value="" selected>Choose Defect</option>@foreach(App\Defect::all() as $defect)<option value="{{$defect->id}}">{{$defect->name}}</option>@endforeach</td>';
html += '<td><input type="file" class="form-control-file" name="image[]"></td>';
html += '<td><input type="text" class="form-control" name="description[]"></td>';
html += '<td><button type="button" class="btn btn-danger btn-sm" id="remove-btn"><i class="glyphicon glyphicon-minus"></i></button></td>';
html += '</tr>';
$('tbody').append(html);
})
});
$(document).on('click','#remove-btn',function () {
$(this).closest('tr').remove();
});
</script>
解决方案
推荐阅读
- python - Python numpy 数组过滤器
- angular - 来自服务工作者的 Angular 504 与谷歌分析/adsense
- php - 使用以下语法声明 PHP var 是否有效:&$var?
- python - 想用lxml和python从某个网站提取链接和标题,但不能
- docker - 带有外部虚拟网络的 Azure API 管理服务到 Docker
- python - Python:创建强制集群布局
- mapbox - Tippecanone 和 Mapbox - 在特定缩放级别丢失细节
- testing - 如何在 java JDBC 中使用键值对读取 db 列值?
- python - 用 np.nan 替换 '-' 将数据类型转换为 pandas 中的浮点数
- c# - Unity 场景间音乐传输