How can I create a nested list of categories in Laravel?
I want to create something like this:
- --- Php
- ------ Laravel
- --------- Version
- ------------ V 5.7
- --- Python
- ------ Django
- --- Ruby
- ..........
My table's fields are:
id | name | parent_id
If I have to add another column like depth, please tell me.
I am using this following code, but I think it is not the best solution for creating nested-list of categories besides, I can not pass this function to my view.
function rec($id)
{
$model = Category::find($id);
foreach ($model->children as $chield) rec($chield->id);
return $model->all();
}
function main () {
$models = Category::whereNull('parent_id')->get();
foreach ($models as $parent) return rec($parent->id);
}