Joe Lowery
Joe Lowery

Reputation: 572

Pass array in Laravel route to Blade template

I'm trying to pass an array of values from routes.php in Laravel to a @foreach loop in a Blade template. Here's my route routine:

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

And my Blade template code:

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

My log is showing that the variable in the template - $theColors - is not undefined. What am I doing wrong?

Upvotes: 3

Views: 4996

Answers (1)

Laurence
Laurence

Reputation: 60058

You have not passed $theColors correctly to the view

Change

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

to

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

Upvotes: 2

Related Questions