Code that causes problem
<code>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.');
}
}
</code>
<code>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.');
}
}
</code>
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
<code>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
)
</code>
<code>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
)
</code>
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