The Closure Table solution is a simple and elegant way of storing hierarchies. It involves storing all paths through the tree, not just those with a direct parent-child relationship.

Creating TreePaths table with plain Comments table

To employ the Closure Table solution, we can create another table, TreePaths, in addition to a plain Comments table, having two columns, each of which is a foreign key to the Comments table.

Get hands-on with 1200+ tech skills courses.