I have the following code in Laravel 5.4.* to store an array of JSON objects.
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);
}
I had an error for a missing value that is not nullable, so I started the debugging.
This is the return of the $samples, just before the encoding:
[
{
"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
}
]
However, if I commented out the return and let the eloquent runs, the insert is messed up.
This is the full error
{"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}}
Edit: A few more details how I reproduce the error. I don't have it on every insert request, but only after a certain process.
I have a dynamic form with Angular where user can create as many models of Sample
as she wants. The first time the user send the request with the models, Laravel saves those without an error.
When I refresh the page and the form is created automatically with the data that I had in the DB, if I remove one of them and create a new one, but keep at least one of the previous data, then I have the error.
Just before the above code, I have a line that delete data from the same table, so everything that was there, isn't during the insert
command.
HTML of the dynamic form:
<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" class="col-md-12">Product</label>
<div class="input-group">
<select name="product" [(ngModel)]="input.product_id" id="product" class="form-control">
<option *ngFor="let product of products" value=""></option>
</select>
</div>
</div>
<div class="col-md-4">
<label *ngIf="i==0" for="supplier" class="col-md-12">Supplier</label>
<div class="input-group">
<select name="supplier" [(ngModel)]="input.supplier_id" id="supplier" class="form-control ">
<option *ngFor="let supplier of suppliers" value=""></option>
</select>
</div>
</div>
<div class="col-md-2">
<label *ngIf="i==0" for="quantity" class="col-md-12">Quantity</label>
<input type="number" min=1 step=1 [(ngModel)]="input.quantity" id="quantity" name="quantity" class="form-control">
</div>
</div>
via Tasos