Back

Database Transaction for static methods


Hi, I've been trying to use database transactions with some static functions I created. But, is this the wrong way of doing it? I can't seem to get the database transaction to rollback on error.

In Controller

        try{
            \DB::beginTransaction();
            $transaction = BankHelper::transfer($authBank, $receiverBank, $amount);
            if ($transaction != -1) {
                BankHelper::writeFeed($authUser, $receiver, $request->input('title'), 
                    $request->input('description'), $transaction);
            } 
            \DB::rollback();
        } catch (\Exception $e) {
            \DB::rollback();
            \Log::error($e);
            return response()->json('Could not deliver the Mo:Mo', 500);
        }

In BankHelper.php

    public static function transfer(Momobank $sender, Momobank $receiver, $amount) {

        $transaction = -1;
        if ($sender->raw < $amount)
            return -1;

        $receiver->cooked = $receiver->cooked + $amount;
        $receiver->save();
        $sender->raw = $sender->raw - $amount;
        $sender->save();
        $transaction = BankHelper::writeTransaction($sender, $receiver, $amount, true);

        return $transaction;
    }

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us