43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Course extends Model
|
|
{
|
|
protected $guarded = [];
|
|
public $appends = [
|
|
'paid_amount'
|
|
];
|
|
|
|
public function category(){
|
|
return $this->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 ,
|
|
);
|
|
}
|
|
}
|