I have thought about doing something like this...
Did you try it?
I usually go for closure tables for hierarchical data on SQL.
I am not aware of any PHP implementations as I usually implement them on the projects I use it (or copy from a project to other if it is for the same client). In this pattern you keep an auxiliary table that maps the hierarchies between your records and have a depth column.
Some articles in case you got interested:
- https://karwin.com/blog/index.php/2010/03/24/rendering-trees-with-closure-tables/
- https://dirtsimple.org/2010/11/simplest-way-to-do-tree-based-queries.html
And a book reference (from the author on first link above) where I got to know this pattern:
Unfortunately I cannot share the trait I wrote it for commissioned projects for clients.
Implementing it is not hard. Hardest parts are deleting or moving nodes. Inserting is easy enough and the great advantages are selecting tree paths and limiting depth.