将数组通过Laravel路由传递到Blade模板

3

我正在尝试将routes.php中的值数组传递到Blade模板中的@foreach循环。这是我的路由例程:

Route::get('/', function()
{
    $theColors = array("red","green","blue","yellow");
    return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors'));
});

以下是我的 Blade 模板代码:

    @foreach ($theColors as $color)
    <p>The color is {{$color}}.</p>
    @endforeach

我的日志显示模板中的变量$theColors没有被定义。我做错了什么?

1个回答

2

您没有正确地将$theColors传递给视图

请更改为

return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors'));

为了

return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors' => $theColors));

完美 - 我知道我漏掉了一些基础的东西。谢谢! - Joe Lowery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接