Example of Database Transactions in akaunting/akaunting

File app/BulkActions/Banking/Reconciliations.php (link to Github)
use App\Abstracts\BulkAction;
use App\Models\Banking\Reconciliation;
use App\Models\Banking\Transaction;

class Reconciliations extends BulkAction
{
    //
    public function reconcile($request)
    {
        $reconciliations = $this->getSelectedRecords($request);

        foreach ($reconciliations as $reconciliation) {
            \DB::transaction(function () use ($reconciliation) {
                $reconciliation->reconciled = 1;
                $reconciliation->save();

                Transaction::where('account_id', $reconciliation->account_id)
                    ->isNotReconciled()
                    ->whereBetween('paid_at', [$reconciliation->started_at, $reconciliation->ended_at])->each(function ($item) {
                        $item->reconciled = 1;
                        $item->save();
                    });
            });
        }
    }
    //
}