DEV Community

Discussion on: How to create a nested-list of categories in Laravel blade

Collapse
 
monaye profile image
Monaye Win

how do you limit the numbers of categories(total of both parent and child) to show on the page?

Collapse
 
alinchindea profile image
Alin Chindea

you paginate. where you do $categories = Category::whereNull('parent_id')->with('childs')->get(); you can do $categories = Category::whereNull('parent_id')->with('childs')->paginate(x); where x is the number of items you want to show on page.

Collapse
 
monaye profile image
Monaye Win

that will do pagination based on the count of the parent_id category.
I wanted to display the categories with a hierarchy. Ended up with this:

$categories = Category::where('level', 0)->with('descendants')->get()->flattenTree()->paginate(15);
Enter fullscreen mode Exit fullscreen mode

Image of hierarchy

Collapse
 
monaye profile image
Monaye Win

that will do pagination based on the count of the parent_id category.
I wanted to display the categories with a hierarchy. Ended up with this:

$categories = Category::where('level', 0)->with('descendants')->get()->flattenTree()->paginate(15);
Enter fullscreen mode Exit fullscreen mode