【问题标题】:Laravel private method returns null but with variable but returns proper response with hardcoded intLaravel 私有方法返回 null 但带有变量,但返回带有硬编码 int 的正确响应
【发布时间】:2020-05-01 20:50:48
【问题描述】:
    private function subtractCharges($amount){
        if($amount < 50000){
            // charge 3% only
            $sum = $amount - (0.03 * $amount);
            return $sum;
        } elseif($amount >50000 && $amount < 250000){
            // charge 3% + 50 naira
            $sum = $amount - ((0.03 * $amount) + 5000);
            return $sum;
        } elseif ($amount > 300000){
            // charge 3% + 150 capped at 3000
            $charge = ((0.03 * $amount) + 15000);
            $sum = $charge > 300000 ? $amount - 300000 : $amount - $charge;
            return $sum;
        }
    }

在下面的代码中使用时返回null:

    public function giveFromWallet($org_id, $amount, $name, $purpose){
        // check if sufficient funds in wallet to give
        $amountKobo = $amount * 100;
        if ($this->balance() >= $amountKobo) {
            // deduct the product->price from user wallet
            if(DB::transaction(function () use ($org_id, $amountKobo, $name, $purpose) {
                // deposit to the user
                DB::table('transactions')->insert([
                    'organisation_id' => $org_id,
                    'user_id' => auth()->user()->id,
                    'admin_id' => 'nill',
                    'amount' => $this->subtractCharges($amountKobo),//error happens here
                    'user_amount' => $amountKobo,
                    'name' => auth()->user()->name,
                    'email' => auth()->user()->email,
                    'purpose' => $purpose,
                    'type' => 'Deposit',
                    'auth_code' => 'nill',
                    'created_at' => Carbon::now()
                ]);
                // add revenue/earnings
                $this->revenue($this->calculateRevenue($amountKobo), auth()->user()->id, $org_id, $name);
                // deduct the amount from the user's account
                $this->deduct(auth()->user()->id, $amountKobo, $name, $purpose);
            }, 1)){
                return back()->with('status', 'You have given '.$this->toNaira($amountKobo).' to '.$name);
            }else{
                return back()->with('error', 'Transaction incomplete, contact admin');
            };
        } else {
            return back()->with('error', "Transaction failed, Insufficient funds!");
        }
    }

此外,如果交易失败,但当我将 $this->deduct()$this->revenue() 移到交易,他们通过而 DB::table('transactions') 失败。

提前致谢!

【问题讨论】:

    标签: php laravel transactions insert


    【解决方案1】:

    如果 subtractCharges() 函数中的 $amount 等于 50000,或者如果它介于 250000 和 300000 之间,则该函数不会返回任何内容(或 null)。也许这就是问题所在?

    【讨论】:

    • @Elvis 当$amount 等于 50000 或$amount >= 250000 和 subtractCharges() 函数不返回值。这些 if 检查并没有涵盖所有可能的@ 987654326@ 值。我不知道该怎么解释。
    • 好的,我现在明白了。谢谢你。你明白为什么一个事务失败时不会回滚其他事务吗?
    • 对你的答案投了赞成票,但不幸的是我的票还没算
    • 我不确定事务问题,但如果其中一个查询引发异常,则应全部回滚。但请注意,DB::transaction 不会返回 true/false 来表示成功。我认为您应该将其包装在 try/catch 块中以捕获它在失败时引发的异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2016-06-05
    • 2017-06-11
    相关资源
    最近更新 更多