Php 试图向视图传递数据时未定义变量-Laravel 5
我试图在我的所有页面上都包含一个视图(代码如下),因此我将其包含在布局模板中(因此它可以在所有页面上呈现)。不幸的是,当我尝试运行任何页面时,会出现以下错误: 未定义变量:站点(视图: /Users/Documents/audit/resources/views/layouts/check.blade.php) 查看(check.blade.php):Php 试图向视图传递数据时未定义变量-Laravel 5,php,laravel,blade,laravel-5.2,laravel-blade,Php,Laravel,Blade,Laravel 5.2,Laravel Blade,我试图在我的所有页面上都包含一个视图(代码如下),因此我将其包含在布局模板中(因此它可以在所有页面上呈现)。不幸的是,当我尝试运行任何页面时,会出现以下错误: 未定义变量:站点(视图: /Users/Documents/audit/resources/views/layouts/check.blade.php) 查看(check.blade.php): @if (count($sites) == 0) // Removed as it is irrelevant. @endif public
@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
控制器:
@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
我尝试在其中包含视图(显示错误):
@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
注意:我没有在路线中添加任何与布局相关的代码。检查页面
非常感谢您的帮助。试试这个
@if(isset($sites)&&count($sites) == 0)
// Removed as it is irrelevant.
@endif
注意:
@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
当您使用
@include('layouts.check')
时,您没有设置$sites
问题是,当您包含文件布局时。check
方法siteCheck()
未启动(因此变量$sites不存在)
您有两种选择:
@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
@include('layouts.check',['sites'=>$sites])
(仍然需要将$sites
从控制器传递到主视图。)
public function boot()
{
view()->composer('layouts.check', function ($view) {
$view->with('sites', Site::where('user_id', Auth::id())
->get());
});
}