当我在前面的站点中传递变量时,它表明该变量为undefined
:为什么会发生这种情况?我也尝试过使用紧凑型,但是它对我不起作用。有什么办法可以解决这个问题?
HomeController
文件代码:
public function index()
{
$category =DB::table('categories')->where('status', 1)->get();
$brand =DB::table('brands')->where('status', 1)->get();
$home=view('home.main_content');
return view('home')->with('home', $home)->with('category', $category);
}
使用foreach
循环的前置代码:
@foreach($category as $result)
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><a href="#">{{$result->name}}</a></h4>
</div>
</div>
@endforeach
错误:
Undefined variable: category (View: C:\xampp\htdocs\laravelblog\resources\views\home\sidebar_left.blade.php)
答案 0 :(得分:0)
您的视图称为home
,而您尝试使用该变量的视图称为sidebar_left
。
因此,如果使用Blade @include
指令加载该视图,则需要将变量传递给该视图。
您可以这样做:
@include('sidebar_left', ['category' => $category])
然后您将可以使用它。