Error: Unsupported operand types: Illuminate\Database\Eloquent\Collection - int

Error: Unsupported operand types: Illuminate\Database\Eloquent\Collection - int

我有这段代码,我将数据插入 table 以及用于算术运算的数据。

public function Repay (LoanApplication $loanApplication, transLog $transaction,  Request $request) {
    abort_if(!Auth()->user()->is_admin, Response::HTTP_FORBIDDEN, '403 Forbidden');
    
    
    $data['Loan_id']    = $loanApplication->id;
    $data['Cust_id']    = $loanApplication->cust_id;
    $data['trans_type'] = "Credit";
    $data['Amount']     = $request['Amount'];
    
    transLog::create($data);

    
    $transact = $transaction::where('Loan_id', $loanApplication->id);
    
    $curr_amount = $transact->Where('trans_type', '=', 'debit')->get('Amount');
    $curr_repayment = $transact->where('trans_type', '=', 'credit')->sum('Amount');
    $bals = $curr_amount - $curr_repayment;

    if($bals == 0){
        $loanApplication->update([
            'status_id' => 18
        ]);

        return view('admin.loanApplications.index', compact('loanApplication', 'transact'))->withMessage('Repayment Successful!');
    }

创建函数正在运行,但我在第

行收到上面的错误
$bals = $curr_amount - $curr_repayment;

知道这是为什么吗?

$curr_amount = $transact->Where('trans_type', '=', 'debit')->get('Amount');

这里的get()语句总是returns一个数组。

$curr_repayment = $transact->where('trans_type', '=', 'credit')->sum('Amount');

sum()语句returns一个数字(整数或浮点数)。

所以显然这两个操作数不能相加。

此外,您可能应该像这样定义 $transact,因为您滥用 $transaction 变量作为查询实例,而它似乎指的是模型:

 $transact = transLog::query()->where('Loan_id', $loanApplication->id);

(同时重构您的代码以使用 CamelCase:TransLog