Laravel抱怨缺少不可空的价值,但它存在

时间:2017-04-11 11:59:44

标签: json laravel angular laravel-5

我在Laravel 5.4。*中有以下代码来存储JSON对象数组。

Sample::where('contest_id', $request->get('contest_id'))
                ->where('type', '0')
                ->delete();
if ( $request->get('extra3') ) {
   $samples = $request->get('samples');
   //return $samples;
   Sample::insert($samples);
}

我的错误值是一个不可为空的缺失值,所以我开始调试。

这是$ samples的返回,就在编码之前:

[
  {
    "id": 16,
    "contest_id": "35",
    "product_id": "2",
    "supplier_id": "2",
    "quantity": "5",
    "type": "0",
    "created_at": null,
    "updated_at": null
  },
  {
    "contest_id": 35,
    "type": "0",
    "product_id": "4",
    "supplier_id": "3",
    "quantity": 3
  }
]

然而,如果我注释掉了回报并且让雄辩说出来了,那么插入就会搞砸了。

这是完整的错误

{"error":{"message":"SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: samples.supplier_id (SQL: insert into \"samples\" (\"contest_id\", \"created_at\", \"id\", \"product_id\", \"quantity\", \"supplier_id\", \"type\", \"updated_at\") select 1 as \"contest_id\", as \"created_at\", 5 as \"id\", 2 as \"product_id\", 5 as \"quantity\", 2 as \"supplier_id\", 0 as \"type\", as \"updated_at\" union all select 1 as \"contest_id\", 1 as \"created_at\", 1 as \"id\", 1 as \"product_id\", 0 as \"quantity\", ? as \"supplier_id\", ? as \"type\", ? as \"updated_at\")","code":"23000","status_code":500}}

编辑:更多细节我如何重现错误。我没有在每个插入请求上,但只在某个过程之后。

我有一个使用Angular的动态表单,用户可以根据需要创建尽可能多的Sample模型。用户第一次使用模型发送请求时,Laravel会保存那些没有错误的请求。

当我刷新页面并使用我在数据库中拥有的数据自动创建表单时,如果我删除其中一个并创建一个新表单,但保留至少一个以前的数据,那么我有错误。

在上面的代码之前,我有一行从同一个表中删除数据,所以那里的所有内容都不在insert命令中。

动态表单的HTML:

<div class="row col-md-12" *ngFor="let input of initSamples; let i=index">
     <div class="col-md-4">
          <label *ngIf="i==0" for="product{{i}}" class="col-md-12">Product</label>
          <div class="input-group">
               <select name="product{{i}}" [(ngModel)]="input.product_id" id="product{{i}}" class="form-control">
                     <option *ngFor="let product of products" value="{{product.id}}">{{product.name}}</option>
               </select>
          </div>
     </div>
     <div class="col-md-4">
          <label *ngIf="i==0" for="supplier{{i}}" class="col-md-12">Supplier</label>
          <div class="input-group">
               <select name="supplier{{i}}" [(ngModel)]="input.supplier_id" id="supplier{{i}}" class="form-control ">
                     <option *ngFor="let supplier of suppliers" value="{{supplier.id}}">{{supplier.name}}</option>
               </select>
         </div>
    </div>
    <div class="col-md-2">
         <label *ngIf="i==0" for="quantity{{i}}" class="col-md-12">Quantity</label>
         <input type="number" min=1 step=1 [(ngModel)]="input.quantity" id="quantity{{i}}" name="quantity{{i}}" class="form-control">
    </div>
</div>

0 个答案:

没有答案