Menu

1 / 2

Why I Stopped Using Raw SQL Date Functions and Switched to Carbon in Laravel

DEV Community·Chris Lloyd Fallaria·6 months ago
#MF760Hn2
#laravel#webdev#php#carbon#date
Reading 0:00
15s threshold

It Started With a Bug

When I was building VMMS — a voucher management system

for government offices — everything worked fine locally.

MySQL. Clean queries. Fast results.

Then I deployed to a server running MariaDB.

Half my charts broke.

The Problem

I had written date queries like this all over the codebase:

// This breaks on MariaDB DB :: table ( 'voucher_transactions' ) -> selectRaw ( 'MONTHNAME(created_at) as month, COUNT(*) as total' ) -> groupByRaw ( 'MONTH(created_at), MONTHNAME(created_at)' ) -> get ();

Enter fullscreen mode

Exit fullscreen mode

MONTH() and MONTHNAME() are MySQL functions. They work

fine on MySQL but behave differently on MariaDB — especially

when combined with GROUP BY .

The result? Months showing up in the wrong order.

Duplicate entries. Missing data.

Why This Happens

MySQL and MariaDB have diverged over the years.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.