[英]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?我想在有新数据时动态循环(foreach)这些数据,我该怎么做? I use the package laravel-nestedable .我使用包laravel-nestedable 。
This is my return JSON, which is dynamic:这是我的返回 JSON,它是动态的:
[
{
"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:您的 Json 数据似乎格式不正确,但如果没有问题,那么您可以将您的 Json 数据从 Controller 发送到 View,如下所示:
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 来获取如下所有数据:
@foreach($json as $data)
{{ $data->item }}
@endforeach
You can also convert your Json data to array and then send like below:您还可以将您的 Json 数据转换为数组,然后发送如下:
$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 来获取如下所有数据:
@foreach($array as $data)
{{ $data['item'] }}
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.