You can leverage recursive common table expressions CTEs to achieve this. Thankfully, there is a package by Staudenmeir to achieve this with Eloquent Models allowing you to define the relationships in familiar manner, and specifying the depth if needed.
Feb 6, 2025
2
Level 2
Make category tree
have a task to make category tree I don't no how many parent or children add
example:
- Parent 1
- children 1
- children 5
- children 8
- children 6
-Parent 2
in Service Provider use Model::shouldBeStrict();
when use filamentphp ineed to display like this:
Parent 1 > children 1 > children 5 > children 8
ok, really I don't have any idea to do this I try many way but still failed
Model:
protected $fillable = [
'name',
'status',
'parent_id',
'sort',
];
public function children(): HasMany
{
return $this->hasMany(related: self::class, foreignKey: 'parent_id');
}
public function parent(): BelongsTo
{
return $this->belongsTo(related: self::class, foreignKey: 'parent_id');
}
Level 104
1 like
Please or to participate in this conversation.