Skip to content
Advertisement

How to make unlimited category json listing?

I want to get unlimited json loop with category parent_id but I couldn’t find how to do that. Do while I searched but. I couldn’t find the source. How can I do the foreach loop do while? Can you guide me on this?

JavaScript

Json result;

JavaScript

Laravel code;

JavaScript

Advertisement

Answer

A recursive map operation seems perfect for this situation.

If you don’t mind having element with empty children in your json ('children': []), then this should work.

JavaScript

If you don’t want that, then adding a bit of logic is needed.

JavaScript

Finally simply return the results.

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