0

我正在尝试在 Laravel 刀片/视图中设置分页,但它在以下消息中显示错误:

坏方法调用异常

方法 Illuminate\Database\Eloquent\Collection::paginate 不存在。

控制器

public function view()
{
    $user = Auth::user();
    $basic_info = User::find($user->id)->basic_info;
    $category = Category::all()->paginate(10); 

    return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}

查看刀片(admin.article.category-view)

<div class="panel-body">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>Category Name</th>
        </tr>
        </thead>
        <tbody>
        @foreach($category as $cat)
            <tr>
                <td>{{ $cat->name }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
    {{ $category->links() }}
</div>
4

2 回答 2

4

仅在查询构建器或 Eloquent 查询上使用分页方法,而不是在集合上使用,如下所示:

public function view()
{
    $user = Auth::user();
    $basic_info = User::find($user->id)->basic_info;
    $category = Category::paginate(10); 

    return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
于 2019-01-14T07:58:55.513 回答
1

您需要删除all()

 $category = Category::paginate(10); 

当您使用时,all()您会从表中获取所有行并获取一个集合

您只能"paginate"在 a上调​​用Query,而不能在 a上调​​用Collection

于 2019-01-14T08:32:41.357 回答