Files
Virtnet.bond/app/Models/Category.php
2025-11-30 11:57:22 +01:00

43 lines
965 B
PHP

<?php
namespace App\Models;
use App\Observers\CategoryObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[ObservedBy(CategoryObserver::class)]
class Category extends Model
{
use HasFactory;
protected $guarded = [];
/**
* Get the products of the category.
*/
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
/**
* Get the parent category of the category.
*/
public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}
/**
* Get the children categories of the category.
*/
public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id');
}
}