首页 > 解决方案 > 使用 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>

标签: javascriptphpdatabaselaravel

解决方案


推荐阅读