1

I'm using Laravel 5.2. In routes.php, I have:

Route::get('test', function() {
    $events = App\Event::get();
    return view('test', $events);
});

In test.blade.php, I have:

<?php print_r($events) ?>

And I get:

ErrorException in ....php line 11: Undefined variable: events (View: .../test.blade.php)

My get() statement is working properly (print_r($events) works in routes.php). Why isn't it getting passed to the blade template?

2 Answers 2

3

You are not passing the data correctly. Try this instead:

return view('test', ['events' => $events]);

You have other options too. For example:

return view('test', compact('events'));

return view('test')->with(['events' => $events]);

Source: https://laravel.com/docs/5.2/views

Sign up to request clarification or add additional context in comments.

2 Comments

Just becareful linking people to 'master' docs as that is the unreleased next version of laravel.
Thanks, that works! Kinda wonky that you have to pass it that way, no?
1
return view('test', ['events' => $events]);

you can also display value like this in blade (for testing)

{{ dd($events) }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.