2021-07-04 01:44:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Payment;
|
|
|
|
|
2022-01-02 12:32:57 +01:00
|
|
|
use App\Fee;
|
2021-07-04 01:44:41 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-01-02 12:32:57 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2022-12-13 23:11:32 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2021-07-04 01:44:41 +02:00
|
|
|
|
|
|
|
class Subscription extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $fillable = ['name', 'amount', 'fee_id'];
|
|
|
|
|
2022-12-13 23:11:32 +01:00
|
|
|
public function getAmount(): int
|
|
|
|
{
|
|
|
|
return $this->children->sum('amount');
|
|
|
|
}
|
|
|
|
|
2022-03-11 20:19:17 +01:00
|
|
|
public function fee(): BelongsTo
|
|
|
|
{
|
2021-07-04 01:44:41 +02:00
|
|
|
return $this->belongsTo(Fee::class);
|
|
|
|
}
|
2022-12-13 23:11:32 +01:00
|
|
|
|
|
|
|
public function children(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(SubscriptionChild::class, 'parent_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function booted(): void
|
|
|
|
{
|
|
|
|
static::deleting(fn ($model) => $model->children()->delete());
|
|
|
|
}
|
2021-07-04 01:44:41 +02:00
|
|
|
}
|