ekpono
154
2
Laravel

How to exit out of saving a record if a condition is not true

Posted 7 months ago by ekpono

I want to save a record in my table but i have set a condition that must be met before it is saved. I don't know if this is the best approach.

 public function transaction($data, $loanTransaction)
    {
        $loanTransaction = find($data['loan_id']);
        $loanTransaction = new LoanTransaction;
        $loanTransaction->decrement($loanTransaction['duration']);

        if(! $loanTransaction['amount'] === $loanTransaction->amount){
            die          
        }

        $loanTransaction->paid_date = $loanTransaction['paid_date'];
        $loanTransaction->payment_method = $loanTransaction['payment_method'];
        $loanTransaction->bank_name = $loanTransaction['bank_account_id'];
        $loanTransaction->save();

        return true;
    }

I want to make sure the amount entered is the same with the one in the database before saving. Am i to die or return false please which approach is the best way to go.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.