将 Laravel 路由中的数组传递给 Blade 模板

Posted

技术标签:

【中文标题】将 Laravel 路由中的数组传递给 Blade 模板【英文标题】:Pass array in Laravel route to Blade template 【发布时间】:2014-07-17 04:37:20 【问题描述】:

我正在尝试将一组值从 Laravel 中的 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】:

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

改变

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

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

【讨论】:

完美 - 我知道这是我所缺少的基本内容。谢谢!

以上是关于将 Laravel 路由中的数组传递给 Blade 模板的主要内容,如果未能解决你的问题,请参考以下文章