Nevda's avatar
Level 1

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)
                })
        }
}
0 likes
6 replies
Sinnbeck's avatar

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

Nevda's avatar
Nevda
OP
Best Answer
Level 1

@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

Nevda's avatar
Level 1

@Sinnbeck Yes indeed it now orders based on ASC and DESC. Thanks for your time anyway.

Snapey's avatar

your order by is on the user not the customers

Im guessing it's the customers you want ordered. you can do this by adding the order to the relationship

public function customers () {
    return $this->hasMany(Customer::class, 'id')->latest();
}
Nevda's avatar
Level 1

@Snapey @Snapey @Snapey Yes. Thanks for your answer. I tried this one to but didn't work. As I updated the question and I put codes inside VueX store file, I realized I used unshift which made this problem. So I changed it to push().

Please or to participate in this conversation.