Laravel-mongodb: Pagination with orderBy

Created on 8 Jun 2016  路  9Comments  路  Source: jenssegers/laravel-mongodb

Member::where('status', 'A')->orderBy('code', 'desc')->paginate(15).
I am using ->paginate(15) with using ->orderBy('code', 'desc').
But the result in not correct. Some of the record is missing and some of the records is duplicate.

Most helpful comment

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
//......

$perPage = 15;
$currentPage = $request->input('page', 1);
$offset = ($currentPage - 1) * $perPage;
$total = Member::where('status','A')->count();
$result = Member::where('status','A')->orderBy('code','desc')->offset($offset)->limit($perPage)->get();
$members = new LengthAwarePaginator($result,$total,$perPage,$currentPage,[
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page']);

All 9 comments

The same happens with latest()->paginate(5)
Order spoofs end results from the pagination.
https://github.com/jenssegers/laravel-mongodb/issues/437 still an issue

Paginate with orderBy having same issue.
Missing some records and some are duplicated.

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
//......

$perPage = 15;
$currentPage = $request->input('page', 1);
$offset = ($currentPage - 1) * $perPage;
$total = Member::where('status','A')->count();
$result = Member::where('status','A')->orderBy('code','desc')->offset($offset)->limit($perPage)->get();
$members = new LengthAwarePaginator($result,$total,$perPage,$currentPage,[
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page']);

@puzc1993 thanks!

@puzc1993,

How we will render this pagination in view files. Can you give example code for this.

@msankar1991
https://laravel.com/docs/5.4/pagination#displaying-pagination-results

@puzc1993 Thanks!

@puzc1993 thank you so much

$members = new LengthAwarePaginator($result,$total,$perPage,$currentPage,[
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page']);

You are saved my life 馃憣

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomartailored picture tomartailored  路  3Comments

geofflancaster picture geofflancaster  路  3Comments

sebastiaanluca picture sebastiaanluca  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments

yupangestu picture yupangestu  路  3Comments