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

vincent15000's avatar

Date casting in the model

Hello,

I'm once again in trouble with date casting.

If I cast the start and end dates, if displays the dates in the right format

protected $casts = [
    // 'start_date' => 'date:d/m/Y',
    // 'end_date' => 'date:d/m/Y',
    'archived' => 'boolean',
];

I'm lost among the different protected properties ($dateFormat, $cast, $dates, ...).

Until today, to get the date in the french format (d/m/Y), I used an appended attribute and it worked fine.

I have seen that the casting does the job to display the dates in my local format.

<el-date-picker
  v-model="form.start_date"
  type="date"
  :editable="false"
  :clearable="false"
  format="DD/MM/YYYY"
  value-format="YYYY-MM-DD"
  placeholder="Choisir une date de début">
</el-date-picker>

The problem is that the casting acts for each time I need to retrieve a date value. It displays the date in the right format, but when I need to edit a date, the date is sent to the form, it is sent in the french format but I need only in this case to have it in english format.

Is it possible to solve this in an easy way ?

Thanks for your help.

Vincent

0 likes
15 replies
vincent15000's avatar

@Sinnbeck The datepicker displays in the right format, the problem is probably because I use the date casting.

If I don't use the date casting, The date is displayed in english format. So to solve this problem, I can use an appended attribute (for example start_date_fr).

Is there a better way to do that ?

Sinnbeck's avatar

@vincent15000 I cast it in laravel as Y-m-d (which isn't American). And handle the display as d-m-Y in the date picker (Javascript)

And you set the format already it seems so I assume it would work?

1 like
vincent15000's avatar

@Sinnbeck I already do that ;).

But in some cases, it is necessary to display the date in another place than a date picker, for example just inside <p> tags. In that case, I receive the date in the english format and it displays in english format. So the only way I found it to write an appended attribute.

Sinnbeck's avatar

@vincent15000 for those cases I have a helper function that displays it in that format :). But if it's on another page, you could use an api resource to format it for that specific page

1 like
vincent15000's avatar

@Sinnbeck I have added this in my model.

public function getStartDateFrAttribute()
{
    $date = Carbon::createFromFormat('Y-m-d', $this->attributes['start_date']);
    return $date->isoFormat('DD/MM/YYYY');
}

public function getEndDateFrAttribute()
{
    $date = Carbon::createFromFormat('Y-m-d', $this->attributes['end_date']);
    return $date->isoFormat('DD/MM/YYYY');
}

Just to show that in this case it's really usefull to retrieve the property using $this->attributes so that the property is exactly what is coming from the database. If I use $this->start_date, I have an error with Carbon (trailing data).

But in another post somebody said that I shouldn't use $this->attributes. What can I do else in my case ?

Sinnbeck's avatar

@vincent15000 you use that when using mutators, not setters. Unless you are already mutating the value and need the original. Personally I would use $this->getOriginal('end_date')

1 like
vincent15000's avatar

@Sinnbeck

$this->getOriginal('start_date') and $this->start_date contain 2022-10-17T00:00:00.000000Z

And $this->attributes['start_date'] contains 2022-10-17.

What I need is 2022-10-17.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You can also just do

return Carbon::parse($this->start_date)->isoFormat('DD/MM/YYYY'); 
1 like
vincent15000's avatar

@Sinnbeck $this->getRawOriginal('start_date') returns the original data from the database.

And Carbon::parse($this->start_date)->isoFormat('DD/MM/YYYY'); is shorter than my first idea.

Thank you very much ;) ... that's fine and it works well now ;).

Please or to participate in this conversation.