DEV Community

Cover image for Ordering dates using DataTables.net in Laravel
Diego
Diego

Posted on

Ordering dates using DataTables.net in Laravel

If you tried to order dates using the DataTables component, you may have struggled for a wile.

I've found the final solution to all these problems: using the data-order attribute to specify the date in yyyy-mm-dd format:

 <td data-order="{{ \Carbon\Carbon::parse($pacient->created_at)->format('Y-m-d H:i') }}">
     {{ $pacient->formatted_created_at }}
</td>
Enter fullscreen mode Exit fullscreen mode

Then, you can simply order by that column in the JavaScript code, and, that's all folks!

PS: I have in my Patient model this definition:

 public function getFormattedCreatedAtAttribute()
    {
        return $this->created_at->format('d-m-Y');
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)