【问题标题】:Pass array in Laravel route to Blade template将 Laravel 路由中的数组传递给 Blade 模板
【发布时间】: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 - 未定义。我做错了什么?

【问题讨论】:

    标签: arrays laravel blade


    【解决方案1】:

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

    改变

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

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

    【讨论】:

    • 完美 - 我知道这是我所缺少的基本内容。谢谢!
    猜你喜欢
    • 2016-02-05
    • 2014-07-17
    • 2019-12-10
    • 2020-10-16
    • 2015-12-02
    • 2020-02-05
    • 2023-03-14
    • 2018-06-20
    • 2015-10-15
    相关资源
    最近更新 更多