'Laravel transaction not rolling back

I'm trying to use Laravel transactions for an operation that requires a change in customer's balance.

In this specific situation, when an already approved (status = 1) process is edited, the update operation must subtract the current process amount and add the new process amount to the customer's wallet.

This is the current code:

    $process = Process::findOrFail($id);
    // CHECKS THE CURRENT PROCESS SUBSIDY
    $old_subsidy = $process->subsidy;

    try {
            DB::beginTransaction();
            $process->update($request->all());

            // SUMS NEW PROCESS SUBSIDY ONLY WHEN PROCESS IS ALREADY APPROVED
            if($process->status == 1) {
                $customer = Customer::findOrFail($process->customer_id);
                $customer->balance = $request->subsidy - $old_subsidy;
                $customer->save();
            }
            DB::commit();

                $notification = array(
                        'title' => __('Success'),
                        'message' => __('Process updated with success'),
                        'class' => 'bg-success'
                );

            return redirect()->route('processes.show',$process->id)->with($notification);

        } catch (\Exception $e) {
            DB::rollback();
             $notification = array(
                    'title' => __('Erro'),
                    'message' => __('Ocorreu um erro'),
                    'class' => 'bg-danger'
            );
            return redirect()->back()->with($notification);
        }

To test, i'm forcing an error, on this line, requesting a customer id that doesn't exist, like: $customer = Customer::findOrFail(99999). The error message is shown but the changes in the process are still being saved instead of roled back.

Am i missing something here?

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source