'How to add a set of data within an array to the database table in Laravel
How to add a set of data within an array to the database table in Laravel?
This is a result return $details_list;
[ { "product_id": "90", "unit": "1", "quantity": "10", "Purchasing_price": "13000", "row_total": "130000" }, { "product_id": "89", "unit": "1", "quantity": "10", "Purchasing_price": "8800", "row_total": "88000" }, { "product_id": "40", "unit": "1", "quantity": "3", "Purchasing_price": "13500", "row_total": "40500" } ]
Tried both ways and it didn't work
$Invoices -> details_invoice -> syncWithoutDetaching($details_list);
$Invoices -> details_invoice -> attach($details_list);
This error appears
Method Illuminate\Database\Eloquent\Collection::syncWithoutDetaching does not exist. or Method Illuminate\Database\Eloquent\Collection::attach does not exist.
Solution 1:[1]
There are several ways, and in this case, if you want to save your record as an array, in Invoices model you can add below lines :
protected $casts = [
'details_invoice' => 'array'
];
or, in your controller :
$Invoices -> details_invoice = serialize($detail_list);
and when you retrieve this item from database, you should unserialize it.
Of course, you need to make sure you've validated everything related to the data, you want to serialize(if the values are from user inputs/request).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Atlas-Pio |
