如何在 Blade 视图中遍历数据 (Laravel 5.2)

how to iterate through data in Blade views (Laravel 5.2)

在 Blade 中,{{ dd(get_defined_vars()['__data']) }} 给我这个输出:

array:6 [▼
  "__env" => Factory {#152 ▶}
  "app" => Application {#2 ▶}
  "errors" => ViewErrorBag {#145 ▶}
  0 => array:1 [▼
    "question" => "question 3"
  ]
  1 => array:1 [▼
    "question" => "question 2"
  ]
  2 => array:1 [▼
    "question" => "question 1"
  ]
]

我的控制器是这样构建这些数据的:

foreach ($questions as $question) {
        $answer = [
            'question' => $question->question,
        ];
        $answers[] = $answer;
    }
    return view('results')->with($answers);

如何在 Blade 中迭代它以显示 3 个问题?

这应该可以完成工作

来自 laravel 文档

When passing information in this manner, $data should be an array with key/value pairs

在控制器中

return view('results')->with('answers', $answers);

在blade

 @foreach($answers as $answer)
         {{ $answer['question'] }}
 @endforeach

看看

https://laravel.com/docs/5.2/views#passing-data-to-views