d2d.emegrendeles.hu/app/Traits/SelfReferenceTraits.php
E98Developer 68b7c35bef git init
2026-02-28 06:53:05 +01:00

38 lines
717 B
PHP

<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
trait SelfReferenceTraits
{
protected string $parentColumn = 'parent_id';
public function parent(): BelongsTo
{
return $this->belongsTo(static::class);
}
public function children(): HasMany
{
return $this->hasMany(static::class, $this->parentColumn);
}
/*
* same level items
*/
public function allChildren()
{
return $this->children()->with('allChildren');
}
/*
* not working!!
*/
public function root()
{
return $this->parent ? $this->parent->root() : $this;
}
}