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

waleedviews's avatar

How to format date with carbon x-text Alpine.js?

I am using apline.js and wanted to format the date using carbon

<td x-text="{{ Carbon\Carbon::parse('item.employee.updated_at')->format('F d Y') }}"></td>

but it gives me an error Could not parse 'item.employee.updated_at': Failed to parse time string

can someone help please?

0 likes
6 replies
webrobert's avatar
Level 51

item.employee.updated_at is alpine?

You'll have to use (something like) momentjs for that, or convert it before you pass it to alpine

1 like
webrobert's avatar

I would either: rethink using alpine here, use a getter for the updated_at, or make an api response, or find a javascript date library to show the dates in alpine.

issamlk's avatar

Hello from the future, I've had similar issue in formating date from laravel model into alpinejs table and instead of adding moment.js which is big library, I've solved it with casting the date from the model side into my format and sending it to alpine fromated as I want.

protected $casts = [
    'birthday' => 'date:Y-m-d',
    'joined_at' => 'datetime:Y-m-d H:00',
];

ps. for more complicated modifications you could also use custom model attributes. I hope someone find this helpful.

Reference https://laravel.com/docs/10.x/eloquent-serialization#customizing-the-date-format-per-attribute

Please or to participate in this conversation.