Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

milosradic's avatar

Problem with pagination in combination with adding new records

Hi all,

I have a problem with my pagination. I have posts that are paginated (10 per page - i have load more button on page and it loads next 10 posts on same page etc.) and users on website can add new posts.

The problem is when User1 load first 10 posts and User2 creates for example 3 posts, then, when User1 loads next 10 posts, first 3 posts in second 10 posts are already loaded in first 10 posts (so i get duplicates in list). Does someone know any workaround for this? (I can try to apply solution either in Laravel or on frontend with javascript and load more button)

I had an idea to pass ID of first post and to query only posts that have ID less than this ID, but my posts are not sorted by ID and that will not work. Actually, my posts can be sorted by created_at (most relevant), by distance (i have lat and long for posts and take users current location to calculate the distance) and by most participants (users can participate to posts). That is a problem because i have to pass different start post ID or some_value so i can query only posts that comes after that one at beginning of post list.

Sorry for bad english and maybe bad explanation but you will figure out what is my problem.

Any suggestions are welcome.

0 likes
3 replies
artcore's avatar

Are they loaded over ajax? You could store the current 10 IDs and have the next call exclude any posts in that array.

milosradic's avatar

It is react on frontend.

Posts are in state. I can filter them by post id, but there is another problem if i load first 10 posts and other users add for example 10 new posts, then i try to load 10 more posts i will have to click 2 times on load more to load next 10 posts or another example if other users add 50 new posts that will bi mess :( In that case it will load posts 4 times (possibly in wrong order i think, because first 10 are loaded and other are added after and will load after and can be in wrong order if you compare them with first 10) and then 5th time it wont load posts because they are already loaded in first 10... you know what i mean...

Buuut, that's one suggestion and i could try to implement something like it (it is better like that than current situation) :D

milosradic's avatar
milosradic
OP
Best Answer
Level 1

I forgot to tell you.

I have implemented your solution with minor modification.

I have stored all IDs and pass them as array in post request to exclude them, but i have also store the max ID and have something like this in my query

$query->where('id', '<', $maxId)

With this max ID i have excluded all new posts added after i load my page (new posts will be loaded only after page refresh).

Please or to participate in this conversation.