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

athenagraphics's avatar

getAttribute not working when i have a number in my column name

Hey guys,

I'm having some trouble with the getAttribute method on a model.

In my database i have the following column names: date_1m, date_6m and date_12m

The problem is that i want to format these items in my model but this isn't working as it should.

This is my function:

public function getDate1mAttribute($value)
{
    return is_null($value) ? $value : Carbon::parse($value)->format('d/m/Y');
}

This isn't formatting my date like it should, because the method is never triggered. Can anyone tell me if this is a laravel bug? Or is it just me doing things wrong?

0 likes
1 reply
JarekTkaczyk's avatar

@athenagraphics It works. I suppose your column might not be null, but rather empty string '' and that's why it isn't formatting.

Change it to:

public function getDate1mAttribute($value)
{
    return ( ! $value) ? $value : Carbon::parse($value)->format('d/m/Y');
}

Please or to participate in this conversation.