adrema/app/Payment/Subscription.php

54 lines
1.1 KiB
PHP
Raw Permalink Normal View History

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;
2022-12-14 00:23:03 +01:00
/**
* @var array<int, string>
*/
2023-12-20 22:11:07 +01:00
public $fillable = ['name', 'fee_id'];
2021-07-04 01:44:41 +02:00
2022-12-13 23:11:32 +01:00
public function getAmount(): int
{
return $this->children->sum('amount');
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Fee, self>
*/
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
2023-02-17 18:57:11 +01:00
/**
* @return HasMany<SubscriptionChild>
*/
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());
}
2023-10-13 15:48:29 +02:00
/**
* @return array<int, array{name: string, id: int}>
*/
public static function forSelect(): array
{
return static::select('name', 'id')->get()->toArray();
}
2021-07-04 01:44:41 +02:00
}