为foreach()提供了无效的参数-Laravel

时间:2018-12-16 10:24:47

标签: php laravel

那是我的控制者:

public function manageCategory() {  
    $categories = Category::all(); 

    return view('admin.category.manageCategory',['categories'=>'$categories']); 
}

我的刀片文件:

@section ('content')    
    @foreach($categories as $category)
        {{$category->categoryDescription}}
    @endforeach
@endsection

保存并刷新页面时,出现错误--->

  

为foreach()提供了无效的参数(查看:   C:\ xampp \ htdocs \ larashop \ resources \ views \ admin \ category \ manageCategory.blade.php)

2 个答案:

答案 0 :(得分:4)

您必须将变量$categories传递给视图,但是您要传递字符串'$categories'

因此,将此['categories'=>'$categories']更改为['categories' => $categories]

答案 1 :(得分:1)

您可以尝试以下方法:

public function manageCategory() {  
    $categories = Category::all(); 
    return view('admin.category.manageCategory')
          ->with(['categories'=>$categories]); 
}

您可以使用此紧凑功能:

public function manageCategory() {  
        $categories = Category::all(); 
        return view('admin.category.manageCategory',compact('categories')); 
    }