adrema/app/Invoice/Models/InvoicePosition.php

41 lines
851 B
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace App\Invoice\Models;
2023-12-17 00:45:03 +01:00
use App\Member\Member;
2023-12-13 00:35:39 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-12-17 00:45:03 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2023-12-13 00:35:39 +01:00
class InvoicePosition extends Model
{
use HasFactory;
public $guarded = [];
2023-12-17 00:45:03 +01:00
/**
* @return BelongsTo<Member, self>
2023-12-17 00:45:03 +01:00
*/
public function member(): BelongsTo
{
return $this->belongsTo(Member::class);
}
/**
* @return BelongsTo<Invoice, self>
*/
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}
public static function booted(): void
{
static::deleted(function ($model) {
if ($model->invoice->positions()->get()->count() === 0) {
$model->invoice->delete();
}
});
}
2023-12-13 00:35:39 +01:00
}