[Chartjs]-Laravel 8: why is sorting by date in eloquent is wrong, using Carbon

3👍

The orderBy() is a Query Builder method. Once you call get(), you get a Collection instance.

To order the records in your DB you need to call orderBy() first, and than get():

UPDATE:
I have included the records count, and date format. You still need to order the records by the violationDateTime column

$data = User::selectRaw('COUNT(*) as violation_count, DATE_FORMAT(violationDateTime, \'%M, %Y\') as formatedDate')
            ->groupBy('formatedDate')
            ->orderBy('violationDateTime')
            ->get();

if your get a Syntax error or access violation: 1055 you need to change the stritc mode in mysql to false in /config/database.php change ['connections' => ['mysql' => ['strict' => false]]]

Leave a comment