spAo's avatar
Level 1

Pagination problem

Hello all! I'm trying to use pagination and it's works fine. But when i add orderBy something is going wrong... Some results partly occurs again when i click on next page... So how can i add orderBy and remove this problem?

0 likes
7 replies
spAo's avatar
Level 1

@Sinnbeck Lol sorry forget to post code : )

$selectedCategory = Course::where('course_category_id', $category->id)->StatusOn()->orderBy('created_at', 'desc')->paginate(18);

and on blade

 {!! $selectedCategory->links() !!}
Sinnbeck's avatar

@spAo What is scopeStatusOn() ?

Other than that it should work. Any chance that the $category->id changes between you changing pages?

1 like
spAo's avatar
Level 1

@Sinnbeck

   public function scopeStatusOn($query)
    {
        return $query->where('status', '1')->whereNotNull('created_at');
    }

It was

->orderBy('id')

problem i added it and now it's working : ) thanks for response

Sinnbeck's avatar

@spAo Good to know. Yeah didnt think about that. Guess my timestamps have never been that close :p

1 like
tykus's avatar
tykus
Best Answer
Level 104

Your query is producing non-deterministic results; add id as a last order field to break ties in created_at sorting:

$selectedCategory = Course::where('course_category_id', $category->id)->StatusOn()->orderBy('created_at', 'desc')->orderBy('id')->paginate(18);
1 like
spAo's avatar
Level 1

@tykus Thanks for help now it's working perfect : )

Please or to participate in this conversation.