I have the following code in Laravel 5.4.* to store an array of JSON objects.
if ( $request->get('extra3') ) {
$samples = $request->get('samples');
//return $samples;
Sample::insert(json_decode($samples, true));
}
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
}
]
The first object was already in the DB and was loaded in an Angular model. The second object was a new added from a form (that's why id and timestamps are missing)
As you can see, the "quantity"
is a string on the first (one returned from HTTP request) and an integer on the second.
However, if I commented out the return and let the eloquent runs, the insert is messed up.
SQL: insert into \"samples\" (\"contest_id\", \"created_at\", \"id\", \"product_id\", \"quantity\", \"supplier_id\", \"type\", \"updated_at\") select 35 as \"contest_id\", as \"created_at\", 16 as \"id\", 2 as \"product_id\", 5 as \"quantity\", 2 as \"supplier_id\", 0 as \"type\", as \"updated_at\" union all select 35 as \"contest_id\", 4 as \"created_at\", 3 as \"id\", 3 as \"product_id\", 0 as \"quantity\", ? as \"supplier_id\", ? as \"type\", ? as \"updated_at\")
Edit:
This is part of the error I have:
Integrity constraint violation: 19 NOT NULL constraint failed: samples.supplier_id
As you can see, Laravel complains for NULL supplier_id. But as you can see the "supplier_id" is in both objects.
via Tasos