问题
I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine
Collection {#240 ▼
#items: array:3 [▼
0 => array:2 [▼
"name" => "desktop"
"views" => "349"
]
1 => array:2 [▼
"name" => "mobile"
"views" => "151"
]
2 => array:2 [▼
"name" => "tablet"
"views" => "68"
]
]
}
This is what I have so far
@foreach($devices as $device)
$key = 0; $key++; $key < 2;
{{ $device[$key] }},
@endforeach
回答1:
@foreach($devices as $device)
{{ $device->name }}
{{ $device->views}}
@endforeach
Will be enough.
回答2:
You need to echo object properties:
@foreach($devices as $device)
{{ $device->name }} has {{ $device->views }}
@endforeach
回答3:
If you like to use key then
@foreach($devices as $key => $val)
{{ $device[$key]->name }},
{{ $device[$key]->views }}
@endforeach
来源:https://stackoverflow.com/questions/41976902/foreach-with-a-multidimensional-array-laravel-blade-templating