【发布时间】:2017-12-14 20:21:11
【问题描述】:
所以,我有这个数组数组,如下所示:
protected static $base = [
[
'id' => 4,
'createdAt' => 1498638933,
'title' => 'Example 1',
'tables' => [
['title' => '#1', 'createdAt' => 1498638940],
['title' => '#2', 'createdAt' => 1498638941],
['title' => '#3', 'createdAt' => 1498638943],
],
],
[
'id' => 7,
'createdAt' => 1498643923,
'title' => 'Example 2',
'tables' => [
['title' => '#1', 'createdAt' => 1498643925],
['title' => '#2', 'createdAt' => 1498643929],
['title' => '#3', 'createdAt' => 1498643932],
],
这些数组在模型代码中,我想在我的页面中显示它们。 另外,来自模型:
public static function getAll() {
sleep(1);
return self::$base;
}
从 web.php(路由),我有:
Route::get('/', function () {
$items = \App\MyModel::getAll();
return view('welcome')->with('items', $items);
});
现在,如果我尝试:
{!! dd($items) !!}
所有数组都显示出来了,但不是很漂亮。我只想要他们的内容。所以,我按照这些步骤(https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating)变成了:
@foreach($items['title'] as $title => $content)
{{ $title }}
@foreach ($content as $tables => $item)
{{ $tables . ' => ' . $item }}
@endforeach
@endforeach
但是“标题”无法识别,我已经尝试过一些类似的方法,但没有任何效果。我可能会错过什么?
【问题讨论】:
-
应该是@foreach($item as $item) {{ $item['title'] }} @endforeach
-
它适用于第一个循环......这个数组里面的数组呢?
-
简单地更新以将那些包含在第一个循环中 - @foreach($item['tables'] as $table) {{ $table['title'] }} @endforeach
-
非常感谢!!成功了!
-
没问题。我已将其添加为答案,因此您可以投票并接受这将关闭关于 SO 的问题
标签: php arrays laravel laravel-5.4 blade