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

kilicabdulkadir's avatar

Localization of date casts on model

I am using the following code on the model.

protected $casts = [
    'outgoing_date' => 'datetime:Y-m-d',
];

But since the application serves different regions, I need to format the date to the region. for example

tr_TR : d.m.Y

en_EN : Y/m/d

..... etc

How can we do localization using cast feature in Laravel?

0 likes
2 replies
kilicabdulkadir's avatar

thank you for your reply @bugsysha

I am using the following code in the get part.

public function getOutgoingDateAttribute( $value ) {
    return (new Carbon($value))->isoFormat('L');
}
public function setOutgoingDateAttribute( $value ) {
    $this->attributes['outgoing_date'] = (Carbon::createFromIsoFormat('L',$value))->format('Y/m/d');
}

But I am getting an error in the Request Validation section while setting.

        'outgoing_date' => [
            'required', 'date_format:' . Carbon::now()->getCalendarFormats()['L']
        ],

how can i set this set and get using Carbon library.

Please or to participate in this conversation.