You should take a look to cursor based pagination. It would be included in the next laravel release take a look here https://github.com/laravel/framework/pull/37216 Until then there are various packages that do the job. I used with success this one https://github.com/juampi92/cursor-pagination
Axios get and pagination offset returning duplicate values
I am broadcasting an event trigger upon user creating a post with pusher, and I'm receiving it in a vue template. For easiness I have added infinite loading.
First loading the posts from the DB and i mounted an Echo listener and pushing to the returned posts array.
However when get request trying to offset the results (pagination to only 5 posts) the 6th result is duplicated (2nd page)
This duplication only happens for the 6th result and all the other results are unharmed
When scrolling down without triggering the event, it works fine, it all happens when the event is triggered, and array is unshifted.
Controller
//get request
public function pusherGet(Request $request)
{
$jobs = Job::orderBy('created_at','desc')->paginate(5);
return response()->json($jobs);
}
view component
<template>
<div>
<h3 class="text-center">All Jobs</h3><br/>
<div class="container">
<div class="card" v-for="(job,index) in jobs" :key="index">
<div class="card-body">
<h4 class="card-title">{{ job.id }}</h4>
<p class="card-text">{{ job.request_type}}</p>
</div>
</div>
</div>
<infinite-loading @infinite="getJob"></infinite-loading>
</div>
</template>
<script>
export default {
data() {
return {
page:1,
jobs: [],
}
},
mounted() {
//this.getJob();
Echo.channel('chat-room.1')
.listen('JobCreated', (e) => {
this.jobs.unshift(e.job);
});
},
methods: {
getJob($state){
axios.get('getjobs', {
params: {
page: this.page,
},
}).then(({data})=>{
if(data.data.length){
this.page += 1;
this.jobs.push(...data.data)
$state.loaded();
}else {
$state.complete();
}
});
}
},
created() {
}
}
</script>
What is the best approach to avoid this duplication ? Any help would be greatly appreciated
Please or to participate in this conversation.