Laravel雄辩的分页不存在错误

时间:2019-01-14 07:55:14

标签: php laravel pagination laravel-pagination laravel-paginate

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

  

BadMethodCallException

     

方法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]);
}

View Blade (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>

2 个答案:

答案 0 :(得分: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]);
}

答案 1 :(得分:0)

您需要删除BrowserWindow.ClearCache(); BrowserWindow.ClearCookies(); BrowserWindow.SetFocus(); BrowserWindow.Refresh();

all()

使用 $category = Category::paginate(10); 时,您将从表中获取所有行并获得一个集合

您只能在all()上调用"paginate",而不能在Query上调用。