简体   繁体   English

Laravel为什么不将我的变量传递给刀片模板?

[英]Why won't Laravel pass my variable to blade template?

I'm using Laravel 5.2. 我正在使用Laravel 5.2。 In routes.php, I have: 在routes.php中,我有:

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

In test.blade.php, I have: 在test.blade.php中,我有:

<?php print_r($events) ?>

And I get: 我得到:

ErrorException in ....php line 11: Undefined variable: events (View: .../test.blade.php) .... php第11行中的ErrorException:未定义的变量:事件(视图:... / test.blade.php)

My get() statement is working properly (print_r($events) works in routes.php). 我的get()语句运行正常(print_r($ events)在route.php中工作)。 Why isn't it getting passed to the blade template? 为什么它没有传递到刀片模板?

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 资料来源: https : //laravel.com/docs/5.2/views

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

您还可以在刀片中显示这样的值(用于测试)

{{ dd($events) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM