KLM113
2 months ago

Nested categories, how to fetch a full branch?

Posted 2 months ago by KLM113

This is not laravel specific but more a logic question.

+------------+--------------+------+-----+---------------------+-------------------------------+
| Field      | Type         | Null | Key | Default             | Extra                         |
+------------+--------------+------+-----+---------------------+-------------------------------+
| id         | int(11)      | NO   | PRI | NULL                | auto_increment                |
| name       | varchar(100) | NO   |     | NULL                |                               |
| locator    | varchar(100) | NO   | UNI | NULL                |                               |
| parent_id  | int(11)      | NO   |     | 0                   |                               |
| level      | tinyint(1)   | NO   |     | 0                   |                               |
| created_at | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
| updated_at | timestamp    | NO   |     | 0000-00-00 00:00:00 |                               |
+------------+--------------+------+-----+---------------------+-------------------------------+

Having this schema and a category id, how can I -in a simple way- retrieve the id of every single category under that branch? There is no limit on how many subcategories can exist under a top level one.

Thanks in advance.

Please sign in or create an account to participate in this conversation.