How are you using it in the frontend? As you are returning json I assume you use it in something like datatables? Be sure that sorting is turned off there
Laravel Pagination returns wrong order!
Hi everyone.
I'm working on this new project, and I'm so confused with this problem.
So, I want to get data using paginate() and before that I want to order data by created_at > ASC.
I see, it returns data with opposite order! No matter if I use DESC or ASC, in both case, result is opposite.
No matter if I order data by created_at or by ID. It always returns from down to top!
I attached the codes I wrote to get and order data and Eloquent relationship I used.
If anyone has any idea why this happens, please help me. Thank you.
Relationship in USER model
public function customers () {
return $this->hasMany(Customer::class, 'id');
}
Get & order data customers data
public function listCustomers () {
$customers = User::with('customers')
->select(
'id',
'fname',
'sname',
'email',
)->orderBy('created_at', 'DESC')->paginate(10);
return response()->json(['customers' => $customers]);
}
And in VUEX Store, inside an action, I fetch ordered data
state: {
customers: [],
loadingCustomers: false
},
getters: {
customers: state => {
return state.customers
},
loadingCustomers: state => {
return state.loadingCustomers
}
}
mutations: {
getCustomers (state, customers) {
return state.customers.unshift(customers)
},
toggleLoadingCustomers (state, status) {
return state.loadingCustomers = status
}
}
actions: {
getCustomers({commit}) {
commit('toggleLoadingCustomers', true)
axios.get('/admin/customers/list').then(response => {
response.data.data.forEach(function (customer, index) {
commit('getCustomers', customer)
});
})
.catch(err => {
console.log(err)
})
.finally(() => {
commit('toggleLoadingCustomers', false)
})
}
}
@Sinnbeck Thanks for your reply. I forgot to post that part of the code; I updated my question with full VUEX Store codes. In fact, I found the problem, I used unshift to put data inside customers[]. So, I changed it to push
Please or to participate in this conversation.