Laravel在查询中分页

时间:2016-09-19 10:22:37

标签: laravel pagination

我想为这个选择做分页,但我不知道我能在哪里做到这一点

  $files = $files->groupBy('file.id_file')->orderBy('data_meet', 'desc')->paginate(30);

我得到了这个错误:

调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: links()

查看:

    <div class="container">
        @foreach ($files as $file)
            {{ $files->file }}
        @endforeach
    </div>
{{ $files->links() }}

1 个答案:

答案 0 :(得分:0)

正如我从你以前的代码中看到的,你的问题是你试图在一个不允许的集合上应用simple pagination试试这个

$files = $files->groupBy('file.id_file')->orderBy('data_meet', 'desc')->get(); 
$currentPage = LengthAwarePaginator::resolveCurrentPage();
        $collection = new Collection($files);
        $perPage = 15;
        $currentPageSearchResults = $collection->slice($currentPage * $perPage, $perPage)->all();
        $result= new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);

然后传递$result而不是$files,其余的仍然是相同的