You can create a relationship which fetches all children recursively:
public function children()
{
return $this->hasMany(Category::class, 'parent')->with('children');
}
Now, whenever you fetch a Category; eager-load the children:
$categories = Category::where('parent',$x)->with('children')->get();
Finally, using a Blade component, you can render the tree:
@foreach($categories as $category)
<x-category :category="$category" />
@endforeach
The component will render the Category properties and recursively render the children. I am using padding-left to indent, but you can change as appropriate:
<div>
{{ $category->name }}
<div style="padding-left: 1rem">
@foreach($category->children as $category)
<x-category :category="$category" />
@endforeach
</div>
</div>