【问题标题】:why show undefined variable when pass frontend为什么在传递前端时显示未定义的变量
【发布时间】:2018-12-23 19:56:07
【问题描述】:

当我在前端站点中传递一个变量时,它显示该变量是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)

【问题讨论】:

    标签: laravel undefined-variable


    【解决方案1】:

    您的视图称为home,您尝试在其中使用该变量的视图称为sidebar_left

    因此,如果您使用刀片 @include 指令加载该视图,您需要将变量传递给该视图。 你可以这样做:

    @include('sidebar_left', ['category' => $category])
    

    然后你就可以使用它了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多