Hello, i want to ask about pagination in laravel. I have middle size table with ~ 5 kk records. I want to use:
$pagination = new LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page
)
As u know first parameter is a collection. I read that I should pass as first param whole eloquent data, then laravel slice those collection to ex. 5 elements (I want see 5 element per page), and then pass result to the view.
Are you trying to do anything in particular? The method from Laravel documentation is not what you are looking for? So on your Controller you can have:
// This gets all Posts, paginates them by 5
$posts = App\Post::paginate(5);
...
return view('post-view', ['posts' => 'posts']);
Then on the view:
<div class="container">
@foreach ($posts as $post)
{{ $post->body }}
@endforeach
</div>
{{ $posts->links() }} <!-- This is the pagination -->
I read that I should pass as first param whole eloquent data, then laravel slice those collection to ex. 5 elements (I want see 5 element per page), and then pass result to the view.