簡體   English   中英

如何使用Laravel Collections實現分頁?

[英]How to Implement Pagination with Laravel Collections?

據我所知,Laravel分頁不適用於Laravel集合(口才)。 例如:

$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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM