【问题标题】:How to calculate bank balance and total banks balance in Laravel 8?如何在 Laravel 8 中计算银行余额和银行总余额?
【发布时间】:2021-07-05 22:31:23
【问题描述】:

我有一对多关系的银行表和交易表。我想计算每家银行的余额以及银行总余额。

银行模式:

class Bank extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'code',
        'accountNumber',
        'swiftCode',
        'currency',
        'country',
        'city',
        'street',
        'zipcode',
    ];

    public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }
}

交易模型:

class Transaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'amount',
        'currency',
        'exchangeRate',
        'reference',
        'type',
        'adjustment',
        'partner_id',
        'bank_id',
    ];
    public function bank()
    {
        return $this->belongsTo(Bank::class);
    }
}

我为余额编写了下面的代码,但尚未正确,因为我无法使用此集合访问交易列。

 <td>{{$bank->transactions->where('type', 'credit')->sum('amount') - $bank->transactions->where('type', 'debit')->sum('amount')}}</td>

有许多货币需要使用 exchangRate 进行标准化。 我需要将数量与 exchangeRate 相乘并将所有结果相加。所以它给出了银行余额。

我应该从索引函数中传递它。应与银行托收一起退回。 BankController 功能索引:

 public function index()
    {
        $banks = Bank::all();
        return view('inc.banks', compact('banks'));
    }

根据这些,我如何计算每个银行余额和银行总余额?

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    你必须从控制器中获取它并将它传递给视图,创建一个函数来获取数据

    //Here You will get all transactions relevant to the bank as per your relationship
    
     $banks =Transaction::with('transactions.bank')->get();
        return view('inc.banks', compact('banks'));
    

    使用输出数组,您可以手动计算所需的数量,或者您可以手动查询它。

    要清楚地了解您的输出,请尝试使用 SQL 语法在数据库上手动查询它。一旦你掌握了正确的 SQL 语法,你就可以使用 laravel eloquent(这个方法将帮助你理解你在做什么)!祝你好运!

    【讨论】:

    • 谢谢@Rifkan Razak,我已经更新了上面的问题。你现在能帮我吗?
    • 实际上你得到了什么输出?你需要做什么?
    • 感谢@Rifkan Razak,代码出错。代替或 Transaction::with... 我把 Bank::with... 和错误解决了。我想将每笔交易的金额和 exchangeRate 相乘,然后将其相加。
    【解决方案2】:

    您可以建立两个关系(贷方和借方),并用它们计算银行余额和总余额。

    银行模型

    public function credits(){
        return $this->hasMany(Transaction::class, foreign_id, primary_id)->where('type', 'credit);
    }
    
    public function debits(){
        return $this->hasMany(Transaction::class, foreign_id, primary_id)->where('type', 'debit);
    }
    

    刀片:

    首先初始化 $total 变量。然后遍历 $banks 以获取每个银行的余额并将它们相加以获得总余额。

    @php( $total = 0) 
    
    @foreach($banks as $key => $bank)
        $balance = $bank->credits->sum('amount') - $bank->debits->sum('amount'); // $balance will have each bank balance 
        $total += $balance;
    @endforeach
    
    {{ $total }} // get total balance for all banks
    

    编辑

    如果您想使用 exchangeRate 计算金额:

    @php($total = 0)
    @foreach($banks as $key => $bank)
        $credits = 0;
        $debits = 0;
        @foreach($bank->transactions as $key => $transaction)
            @if($transaction->type  == 'credit')
                $credits += $transaction->amount*$transaction->exchangeRate;
            @elseif($transaction->type  == 'dedit')
                $dedits += $transaction->amount*$transaction->exchangeRate;
            @endif
        @endforeach
        $balance = $credits-$debits;
        $total += $balance;
    @endforeach
    {{ $total }}
    

    【讨论】:

    • 感谢@Psycho 的回复。我需要在 sum 之前乘以 amount 和 exchangeRate。此代码返回银行数量而不与 exchangeRate 相乘。问题是我不能在合计金额之前将金额和交换率相乘。你能帮忙解决这个问题吗?
    • 我检查了代码并运行它。但不知何故,它没有提供所需的输出。我认为您的代码逻辑是正确的,但它在视图中显示 $total, $credits... 作为文本。我试图更改代码,但结果是一样的。任何方式我已经使用模型解决了这个问题,并发布了它。谢谢@Psycho 的好意。
    【解决方案3】:

    我已经在银行和交易模型中解决了这个问题。我定义了一个自定义属性,然后将其附加到模型中。

    交易模型:

     protected $appends = [
            'final_amount'
        ];
     public function getFinalAmountAttribute() {
            return $this->amount * $this->exchangeRate;
        }
    

    银行模型:

     public function getCreditSumAttribute(){
            $sum = 0;
            if($this->transactions){
                $sum = $this->transactions->where('type', 'credit')->sum('final_amount');
            }
            return $sum;
        }
    
        public function getDebitSumAttribute(){
            $sum = 0;
            if($this->transactions){
                $sum = $this->transactions->where('type', 'debit')->sum('final_amount');
            }
            return $sum;
        }
    
        public function getSubCreditDebitAttribute(){
            return $this->credit_sum - $this->debit_sum;
        }
    

    在银行视图中,我输入了以下代码:

    <td>{{'$ '}}{{$bank->sub_credit_debit}}</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-31
      • 2015-02-18
      • 2021-07-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多