6.6. Transactions
Now let us talk a little about transactions. Without going into the fine detail, I will demonstrate how transactions and the Eloquent ORM can be used together.
DB::transaction(function () {
// Create a new position in the invoice
$line = new App\InvoiceLine();
$line->CUSTOMER_ID = 45;
$line->PRODUCT_ID = 342;
$line->QUANTITY = 10;
$line->COST = 12.45;
$line->save();
// add the sum of the line item to the amount of the invoice
$invoice = App\Invoice::find($line->CUSTOMER_ID);
$invoice->INVOICE_SUM += $line->SUM_PRICE;
$invoice->save();
});
Every parameter of the transaction
method that is located inside the callback function is executed within one transaction.