0

As far as I know that Laravel pagination doesn't work with Laravel collections (Eloquent). For example:

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

I'm trying to make pagination with above code snipped but where('status', 'submitted') is not working.

Controller

$articles = Article::paginate(10);

Blade

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

I am getting an error.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mosharof Zitu
  • 43
  • 1
  • 13

2 Answers2

2

Paginate the query after your where clause:

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

In your blade:

{{ $articles->links() }}

@foreach($articles as $article)
    { ... }
@endforeach
Remul
  • 7,874
  • 1
  • 13
  • 30
1

Adding to @Remul's answer, if you still want to paginate a collection, you could do that with the forPage() method. For example:

$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();
Mozammil
  • 8,520
  • 15
  • 29