首页 > 解决方案 > Column cannot be null by using Auth

问题描述

I'm inserting multiple data into the database. There is an error for my column report_by which is SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'report_by' cannot be null (SQL: insert into 'complaints' ('defect_id', 'image', 'description', 'report_by') values (1, C:\wamp64\tmp\php8E2E.tmp, leak, ?)) Inside column report_by should be the id of the user by using Auth. It's to tell that the complaint made by which users.

users table

id
role
email
typable_id
typable_type

complaints table

id
defect_id
image
description
report_by

ComplaintController.php

<?php

namespace App\Http\Controllers;

use Auth;
use App\Complaint;
use Illuminate\Http\Request;

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' => $request->user_id,
                );
               
                Complaint::insert($data);
            }
        }
        return redirect('/report-form')->with('success','Your report is submitted!');
    }
}

complaint.blade.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() }}
                      <input type="text" class="hidden" id="user_id" value="{{Auth::user()->id}}">
                      <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>

Here my javascript inside the blade

@section('footer')
<script>
    $(document).ready(function () {
        $('#add-btn').on('click',function () {
            var html = '';
            html += '<tr>';
            html += ' <td><input type="text" class="hidden" id="user_id" value="{{Auth::user()->id}}"><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>
@stop

标签: javascriptphpdatabaselaravel

解决方案


推荐阅读