我正在尝试在我的所有页面上包含一个视图(代码如下),所以我已将其包含在我的布局模板中(因此它可以在所有内容上呈现)。不幸的是,当我尝试运行任何页面时 - 会发生此错误:
未定义的变量:站点(查看: /用户/文档/审核/资源/视图/布局/检查.blade.php)
View (check.blade.php):
@if (count($sites) == 0)
// Removed as it is irrelevant.
@endif
控制器:
public function siteCheck()
{
return View::make('layouts.check')
->with('sites', Site::where('user_id', Auth::id())
->get());
}
我尝试包含视图(显示错误)的地方:
@if(!Auth::guest())
@include('layouts.check')
@endif
注:注:我没有在路由中添加任何与布局相关的代码.check页面。
非常感谢您的帮助。
问题是当您包含文件layouts.check
方法时,siteCheck()
不会启动(因此可变$sites不存在)。
您有以下几种选择:
- 包含文件时添加变量
@include('layouts.check', ['sites' => $sites])
(您仍然需要从主视图传递$sites
。
- 添加视图
- 编辑器,每次包含视图时都会添加此变量
参见:https://laravel.com/docs/5.2/views#view-composers
在您的情况下,它看起来像这样:
public function boot()
{
view()->composer('layouts.check', function ($view) {
$view->with('sites', Site::where('user_id', Auth::id())
->get());
});
}
尝试这样做
@if(isset($sites)&&count($sites) == 0)
// Removed as it is irrelevant.
@endif
注意:
当您使用@include('layouts.check')
时,您不会设置$sites