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

usanzadunje's avatar

How to change date serialization logic in Http Resource

Hello guys,

In my resources I am returning date attribute and it gets converted to UTC timezone. I know about changes which took place in Laravel 7 where all dates are converted in ISO format and therefore UTC timezone and that this can be changed by using serializeDate on the model. When I add this method serialization works but only when it is directly returned in response but when I pass the same model to the resource date again gets converted into UTC(ISO format).

From this I am guessing JsonResource has its own serialization logic for dates.

I know I can just do $this->date->timezone(timezone) but I would have to do this in every resource on every date property. I want to change this logic and maybe make it a Trait and just import it whenever I need it, in whichever resource.

Could this be done and how is my question, any help is appreciated.

Regards, Dusan

0 likes
2 replies
usanzadunje's avatar

@vincent15000 It just formats it, the problem is still there. Format is not the issue, conversion to ISO format when serializing resource is. Thanks for reply!

Please or to participate in this conversation.