Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

neeonline's avatar

Date Serialization

Hello,

I want to have all dates returned from my requests to have a specific format. I followed the documentation at https://laravel.com/docs/5.8/eloquent-serialization#date-serialization

So, in my AppServiceProvider inside the method boot() I have:

Carbon::serializeUsing(function ($carbon) {
    return $carbon->format('Y-m-d\TH:i:s.uP T');
});

But all the dates are still returning in the format Y-m-d i:s:u

What am I doing wrong?

Thank you.

0 likes
6 replies
bobbybouwmann's avatar

This only works if you use the correct Carbon namespace

use Illuminate\Support\Carbon;

Also note that this only happens for content that is being parsed to JSON. Not for arrays and other kind of objects.

1 like
neeonline's avatar

@BOBBYBOUWMANN - I'm using the correct Carbon class and calling model()->toJson(). No luck...

I don't know if this matter, I'm using SQLite and this serialization should apply to created_at, updated_at, etc. ($table->timestamps and $table->softDeletes).

Thank you.

bobbybouwmann's avatar

Mmh, SQLite shouldn't make a difference as far as I know. Are you on the latest Laravel version?

1 like
neeonline's avatar

@BOBBYBOUWMANN - Yes, everything last version.

If I force the cast direct in the model, everything works, is just the "global" method that is not working for some reason.

Thank you for your help.

bobbybouwmann's avatar

@neeonline Mmh, if you add any casts in your model it will override the global setup so that is a good thing to check!

Please or to participate in this conversation.