Skip to content
Advertisement

How to print infinity nested categories in dropdown (select) menu in Laravel

I have this category table:

id name parent_id
1 Programming 0
2 History 0
3 PHP 1
4 Javascript 1
6 World history 2
7 jQuery 4
8 AJAX 4
9 Europe 6
10 American 6
16 ajax nested 8

Here is how I get categories in controller:

JavaScript

And Model Category:

JavaScript

I want to output categories, like this:

JavaScript

Which looks like: this

I am considering two options:

  1. Some service and clean php code with recursive function, which to return variable with complete HTML structure (select + options) and display that variable in template file.

  2. Some Laravel way, which I don’t know how to achieve.

Advertisement

Answer

In your main blade template do like below. Here we first add the select box then loop through the categories. If a parent category has childs, then first the childs are added by calling another template and passing childs data to it.

JavaScript

Now, we have to create childs displaying template. Based on my example, the name should be subcategories.blade.php. In the child blade template, add the followings:

JavaScript

In the child template, we are recursively calling the child template itself over and over as long as each child has other childs.

And, here is the outcome on my machine: https://ibb.co/ynRB04h

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement