When working with Laravel, you usually handle dates and times using the Carbon library. If you're using Laravel JSON resources to return structured API responses, you might wonder how Carbon instances are serialized into JSON.

By default, Laravel automatically converts Carbon instances to ISO 8601 formatted strings when they are included in a JSON resource. For example:

use Carbon\Carbon;

return Carbon::now()->toISOString();

This results in a string like:

2024-11-19T12:34:56.789000Z

The toISOString() method ensures that your datetime values are formatted in a standard, widely-used format.