PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] How to get child under sub-category and sub-category user category in JSON Laravel

 January 25, 2022     arrays, laravel-5, loops, php     No comments   

Issue

This very common question but I am not able to make it in JSON Format for API. I a Table Category where I stored three layers of Category. Category->subcategory->child. I want to get this in API in Format.

Something like.

Category

  sub-category-1
  sub-category-2 
      child-2-1
      child-2-2
  sub-category-3

I try with code

  $allData = array();
            // get all parent category 
            $categories = Category::where(['status' => 1, 'parent_id' => 0])->get();        
            foreach ($categories as $key=>$sub) {
                    // now take one by one it's child category 
                    $allData[$key]['parent'] = $sub->name;
                    $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();
                    $subCat = array();
                    // set parent category title
                    foreach ($subCategory as $k=>$subcat) {
                        $subCat[$subcat->id] = $subcat->name;

                    }
                    // set subcategory array
                    $allData[$key]['subcategory'] = $subCat;
            }
           return $allData;

I got this result not I don't know how to get child value

enter image description here

database look like

enter image description here


Solution

Finally, I solved this

       $parents = Category::where('parent_id', 0)->where('status', 1)->orderBy('sort_order', 'asc')->get();
        foreach ($parents as $parent) {
            $childs = Category::where('parent_id', $parent->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
            if (count($childs) > 0) {
                $subCat = array();
                $players = array();
                $roster[$parent->name] = $players;
                        foreach ($childs as $i => $child) {
                            $subchilds = Category::where('parent_id', $child->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
                            if (count($subchilds) > 0) {

                                $roster[$parent->name][$child->name] = $subCat;
                                foreach ($subchilds as $subchild) {

                                    $roster[$parent->name][$child->name][$subchild->id] = $subchild->name;
                                }

                            }else{
                                $roster[$parent->name][$child->name] = $players;
                            }
                        }

            }
        }
        return $roster;

enter image description here



Answered By - Brij Sharma
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing