belongsTo(Category::class); } public function user(){ return $this->belongsTo(User::class, 'student_id'); } public function allTransactions(){ return $this->hasMany(Transaction::class); } public function creditTransactions(){ return $this->hasMany(Transaction::class)->where('type','CREDIT'); } public function debitTransactions(){ return $this->hasMany(Transaction::class)->where('type','DEBIT'); } protected function paidAmount(): Attribute { $creditAmount = $this->creditTransactions->sum('amount'); $debitAmount = $this->debitTransactions->sum('amount'); $total = $creditAmount - $debitAmount; return Attribute::make( get: fn ($value) => $total , ); } }