简体   繁体   中英

How to foreach dynamic JSON nested in Laravel Blade

I want to loop through (foreach) this data dynamically when there is new data, how can I do this? I use the package laravel-nestedable .

This is my return JSON, which is dynamic:

[
   {
      "id":1,
      "name":"food",
      "slug":"food",
      "child":[
         {
            "id":2,
            "name":"cake",
            "slug":"cake",
            "child":[
               {
                  "id":3,
                  "name":"Rainbow Cake",
                  "slug":"rainbow-cake",
                  "child":[

                  ],
                  "parent_id":2
               },
               {
                  "id":4,
                  "name":"Banana Cake",
                  "slug":"banana-cake",
                  "child":[

                  ],
                  "parent_id":2
               }
            ],
            "parent_id":1
         },
         {
            "id":5,
            "name":"Donut",
            "slug":"donut",
            "child":[
               {
                  "id":6,
                  "name":"Hony Donut",
                  "slug":"hony-donut",
                  "child":[
                     {
                        "id":7,
                        "name":"Black Hony Donut",
                        "slug":"black-hony-donut",
                        "child":[

                        ],
                        "parent_id":6
                     }
                  ],
                  "parent_id":5
               }
            ],
            "parent_id":1
         }
      ],
      "parent_id":0
   },
   {
      "id":8,
      "name":"Drink",
      "slug":"drink",
      "child":[
         {
            "id":9,
            "name":"Soda",
            "slug":"soda",
            "child":[
               {
                  "id":10,
                  "name":"Milk Sake",
                  "slug":"milk-sake",
                  "child":[
                     {
                        "id":12,
                        "name":"Mango Juice",
                        "slug":"mango-juice",
                        "child":[

                        ],
                        "parent_id":10
                     }
                  ],
                  "parent_id":9
               }
            ],
            "parent_id":8
         },
         {
            "id":11,
            "name":"Juice",
            "slug":"juice",
            "child":[

            ],
            "parent_id":8
         }
      ],
      "parent_id":0
   }
]

Your Json data not seems to be correctly formatted but if its fine then you can send your Json data from Controller to View like below:

return view('your-view')->with('json', $json);

or

return view('your-view', ['json'=>$json]);

or

return View::make('your-view')->with('json', $json);

Then in your blade file you need to use foreach to get all data like below:

@foreach($json as $data)
    {{ $data->item }}
@endforeach

You can also convert your Json data to array and then send like below:

$array =json_decode( json_encode($json), true);
return view('your-view')->with('array', $array);

Then in your blade file you need to use foreach to get all data like below:

@foreach($array as $data)
    {{ $data['item'] }}
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM