[英]How to foreach dynamic JSON nested in Laravel Blade
我想在有新數據時動態循環(foreach)這些數據,我該怎么做? 我使用包laravel-nestedable 。
這是我的返回 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
}
]
您的 Json 數據似乎格式不正確,但如果沒有問題,那么您可以將您的 Json 數據從 Controller 發送到 View,如下所示:
return view('your-view')->with('json', $json);
要么
return view('your-view', ['json'=>$json]);
要么
return View::make('your-view')->with('json', $json);
然后在您的刀片文件中,您需要使用 foreach 來獲取如下所有數據:
@foreach($json as $data)
{{ $data->item }}
@endforeach
您還可以將您的 Json 數據轉換為數組,然后發送如下:
$array =json_decode( json_encode($json), true);
return view('your-view')->with('array', $array);
然后在您的刀片文件中,您需要使用 foreach 來獲取如下所有數據:
@foreach($array as $data)
{{ $data['item'] }}
@endforeach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.