如何使用 Laravel 集合实现分页



据我所知,Laravel分页不适用于Laravel集合(Eloquent)。例如:

$articles = Article::with('category')->where('status', 'submitted')->get();

我正在尝试使用截取上述代码进行分页where('status', 'submitted')但不起作用。

控制器

$articles = Article::paginate(10);

叶片

@foreach($articles->with('category')->where('status', 'submitted') as $article)
    { ... }
    {{ $articles->links() }} 

我收到错误。

where 子句之后对查询进行分页:

$articles = Article::with('category')->where('status', 'submitted')->paginate(10);

在刀片中:

{{ $articles->links() }}
@foreach($articles as $article)
    { ... }
@endforeach

补充@Remul的答案,如果你仍然想对集合进行分页,你可以使用 forPage() 方法来实现。例如:

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunk = $collection->forPage(2, 3); 
// first argument is the page number 
// second one is the number of items to show per page
$chunk->all();

最新更新