Code that causes problem
foreach ($items as $itemData) {
$item = new PurchaseItem();
$item->internal_product_id = (int)$itemData['internal_product_id'];
$item->quantity = (float)$itemData['quantity'];
$item->total = (float)$itemData['total'];
$item->purchase_id = $purchase->id;
if (!$item->save()) {
throw new Exception('Error saving the purchase item.');
}
}
Table Structure
CREATE TABLE `purchase_items` (
`id` int NOT NULL,
`purchase_id` int DEFAULT NULL,
`internal_product_id` int DEFAULT NULL,
`quantity` decimal(10,2) NOT NULL,
`total` double(10,3) DEFAULT NULL
)
I’ve tried to change the variable name for unique. also ensure the field name . But Results are same & not saving data in Purchase_items
table
2
Answers
Here is the problem i got on the code. In modal all the field was declare in public property that's why getting the error. after remove them it works fine.
Is exampled structure of table final/full?
One of possible problem is absent of AUTO_INCREMENT for PRIMARY KEY (id).
Please, change structure of table to