常规错误:1364字段没有默认值 [英] General error: 1364 Field doesn't have a default value

查看:58
本文介绍了常规错误:1364字段没有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在"pointages"表中插入以下数据元素:矩阵,datep,余额,nbrj高级工作和网站,但这给了我以下错误:

I want to insert into my 'pointages' table the following data elements: matricule, datep, balance, nbrj premium work and site, but it gives me the following error:

一般错误:1364字段矩阵"没有默认值

General error: 1364 Field 'matricule' doesn't have a default value

这是我的代码:

SalarieController

public function addMultiple(Request $request){
           foreach($request->get('items') as $item){
              $pointage = Pointage::create([
             'matricule' => $item['matricule'], 
              'datep' => $item['date'],
             'solde' => $item['salaire'], 
             'nbrj' => $item['nbrj'], 
             'prime' => $item['prime'], 
             'ouvrage' => $item['ouvrage'], 
             'chantier' => $item['chantier']
             ]);
            }
              return redirect("mois");
          }

jQuery代码

  $('.add-all').on('click', function() {
        var items = [];
        let valu = $('#datePicker').val();
       let valu1 = $('#chan option:selected').html();
       let valu2 = $('#ouv option:selected').html();
       ///


      $("tr").each(function(i,r){
        if ( i > 0 && $(r).find("input").first().prop("checked"))
        {  

         // let value = $('#nbr').val();
         let value = parseInt($(r.cells[6]).find('input').val());
          let prime = parseInt($(r.cells[7]).find('input').val());
          let sum = (((parseInt($(r.cells[5]).text()))/24) * value ) + prime;
          $(r).find("input").first().replaceWith('<span style="color: green;font-weight: bolder;">✔</span>');//✓ -

          //items.push({value,prime,sum});
         items.push({"matricule": r.cells[3].innerText,"date" : valu, "salaire": sum, "nbrj" : value , "chantier" : valu1 , "ouvrage" : valu2 , "prime": prime })
              }
             // console.log(items);
                                    });
//ajax 
 $.ajax({ 
         method      : 'POST', 
         url       : 'mois', 
         headers: {
             'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
         },
         data      : {"items":items}, // pass in json format 
         success   : function(data) {
             console.log(data);
         },
         error : function(err){
             console.log(err)
         }
     });

//fin ajax 
      });  

mois.blade.php

<div id='form1'>
    @if ($message = Session::get('success'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
    @endif

     {!! csrf_field() !!}

    <div class="form-group">
     <label>Date:</label>
      <input type="date" id="datePicker" name="date" class="form-control" placeholder="date naissance">
    </div>

    <div class="form-group">
      <label>Select Chantier:</label>
      {!! Form::select('chantier_id',[''=>'--- Select Chantier ---']+$chantiers,null,array('class' => 'form-control','id' => 'chan')) !!}
    </div>

    <div class="form-group">
      <label>Select Ouvrage:</label>
      {!! Form::select('ouvrage_id',[''=>'--- Select Ouvrage ---'],null,array('class' => 'form-control','id' => 'ouv')) !!}
    </div>

    <div class="form-group">
      <label>Nbre de jour</label>
        <input type="text" name="nbr" id='nbr' class="form-control" placeholder="nbre de jour" value="1">
      <span id='error'>Input can not blank</span>
    </div>

    <div class="form-group col-md-offset-5 ">
      <button class="btn btn-success " type="submit" id="hide">valider</button>
    </div>

  </div>

 <div id='form2'>
    <h3>form group 2</h3>
    <h4>DATE : <span id="dateE"></span></h4>
    <h4>chantier : <span id="ch"></span></h4>
    <h4>ouvrage : <span id="ou"></span></h4>
      <div class="form-group col-md-offset-5 ">
        <button class="btn btn-success add-all" type="submit" id="hide">Pointage</button>
      </div>
   <table class="table table-bordered" id="mytable">
        <tr>
            <th>Archive</th>
            <th><input type="checkbox" id="check_all"></th>
            <th>S.No.</th>
            <th>matricule</th>
            <th>nom & prenom</th>
            <th>salaire net</th>
            <th>nbre de jour </th>
            <th>prime</th>
        </tr>
        @if($salaries->count())
            @foreach($salaries as $key => $salarie)
                <tr id="tr_{{$salarie->id}}">
                  <td>archive</td>
                  <td><input type="checkbox" class="checkbox" data-id="{{$salarie->id}}"></td>
                  <td>{{ ++$key }}</td>
                  <td>{{ $salarie->matricule }}</td>
                  <td >{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                  <td><input type="hidden" name="salaire" value="{{ $salarie->salairenet }}">{{ $salarie->salairenet }}</td>
                  <td ><input type="text" class='input2' name="nbreJ" class="form-control" ></td>
                  <td><input type="text" name="prime" class="form-control" value="0"></td>
                </tr>
            @endforeach
        @endif
    </table>
 </div>
<!--</form>-->
</div>

迁移create_pointages_table.php

 Schema::create('pointages', function (Blueprint $table) {
            $table->increments('id'); 
            $table->string('matricule'); 
            $table->date('datep');
            $table->double('nbrj');
            $table->double('prime')->nullable();
            $table->double('solde');
            $table->string('ouvrage'); 
            $table->string('chantier'); 

            $table->timestamps();
        });

推荐答案

要检查的事物.在您的积分模型上,确保已将matricule设置为可填充:

Couple of things to check for. On your Pointages model, make sure you have matricule set as fillable:

protected $fillable = [
    'matricule', 'otherVars',
];

如果在模型上不可填充,则无论您填写什么,Laravel都会向数据库返回一个空值.由于DB字段"matricule"在您的代码中不可为空,因此这可能是错误.

If it isn't fillable on the model, Laravel will return a null value to the database no matter what you fill in. As the DB field 'matricule' is not nullable in your code, this might be the source of the error.

要检查的另一件事是您的中间件在此特定路由上正在做什么.在您的Kernal.php中,Laravel默认使用一个使空字符串为null的中间件.因此,如果您的用户未在表单的matricule字段中键入任何内容-如果此中间件到位,它将使其为null.

The other thing to check on is what your middleware is doing for this particular route. In your Kernal.php, Laravel defaults to have a middleware that makes empty strings null. So, if your user doesn't type anything into the matricule field on your form -- if this middleware is in place, it will make it null.

寻找这个:

protected $middleware = [
    // OtherMiddleware
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

如果在其中,请将其删除(以进行测试).如果该代码现在可以正常工作,则您知道问题出在哪里.

If it is there, remove it (for testing). If the code now works, you know what the issue was.

这篇关于常规错误:1364字段没有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆