如何解决此问题“未定义的变量:类别”?

时间:2019-04-16 11:35:50

标签: php laravel-5

我收到此错误消息,我不知道我的代码有什么问题。

CategoriesController.php

class CategoriesController extends Controller
{
    public function index()
    {
       $categories = Category::all();

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

index.blade.php

foreach ($categories as $category)
    <tr>
        <td>{{$category->idcat}}</td>
        <td>{{$category->cat}}</td>
        <td>
            <a href="edit.html" class="fa fa-pencil"></a> 
            <a href="#" class="fa fa-remove"></a>
        </td>
    </tr>
endforeach

1 个答案:

答案 0 :(得分:3)

这不是刀片模板引擎的工作方式: 您应该在视图中使用@foreach @endforeach:

@foreach ($categories as $category)
          <tr>
            <td>{{$category->idcat}}</td>
            <td>{{$category->cat}}</td>
            <td><a href="edit.html" class="fa fa-pencil"></a> <a href="#" class="fa fa-remove"></a></td>
          </tr>
@endforeach