Return transactions result

If you have a DB transaction and want to return its result, there are at least two ways, see the example

1// 1. You can pass the parameter by reference
2$invoice = NULL;
3DB::transaction(function () use (&$invoice) {
4 $invoice = Invoice::create(...);
5 $invoice->items()->attach(...);
6})
7 
8// 2. Or shorter: just return trasaction result
9$invoice = DB::transaction(function () {
10 $invoice = Invoice::create(...);
11 $invoice->items()->attach(...);
12 
13 return $invoice;
14});

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord