Katerina
Katerina

Reputation: 105

Order by created_at not working with Eloquent models

I have an issue when trying to order records by created_at column when using Eloquent Models. The ordering works by date, but not by time.

Example: if I have 3 records with created_at values:

I have the same output when using orderby('created_at', 'desc') or 'asc'. Ordering however works if I have for example:

Ordering also works when I use raw queries. The issue is present only when I use Eloquent Model.

Why is this happening?

Upvotes: 1

Views: 2383

Answers (3)

Andy Ba
Andy Ba

Reputation: 31

It is better to convert datetime value in PHP into Carbon type for ordering and filtering. Try to convert the values first and then order them.

Upvotes: 0

Katerina
Katerina

Reputation: 105

It wasn't a problem on the server side when getting the data, but on the client side because I used 'dataTables'.

Upvotes: 2

darshan danidhariya
darshan danidhariya

Reputation: 1

<script>
    $(document).ready(function() {
        $('#myTable').DataTable({
            "order": [[ 0, "desc" ]] // Assuming first column is 'Name'
        });
    });
</script>
When using bootrape datatable use this 

Upvotes: 0

Related Questions