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

ThisIsPuzzle's avatar

API: when setting getCreatedAtColumn

Hello there,

I have an API that returns the info of a user, also created_at and updated_at. And it returns in a werid format like: 2021-12-27T12:58:21.000000Z , and from some searching i did, it seems it's because of the Carbon class.

Now, i was searching for a method to return my date as a more readable date. Since i'm doing $user->first() and not getting the attributes independently, i didn't find something to help me. So i remembered that in laravel you can make some methods in your Eloquent model to set, get, modify attributes of the model. And i made a method getCreatedAtColumn that returns 'test' and it changed my date into something like this: 2021-12-23 08:42:50

So my question is why it does that? I'm a little bit confused since i return the method to 'test'

0 likes
9 replies
ThisIsPuzzle's avatar

@Sinnbeck Yes. I've formated the date i am receieving in my API. By adding to casts => ['created_at' => 'datetime:Y-m-d H:i:s'] and i get it correctly.

My question is: why when i made getCreatedAtColumn() method that returns 'test' it returns the date more readable to human? I mean, i have in my code: return 'test' in the getCreatedAtColumn() and the format of the date changes.

Sinnbeck's avatar

@ThisIsPuzzle getCreatedAtColumn() is used to get the column in the database. I dont assume the column is named test?

/**
     * Get the name of the "created at" column.
     *
     * @return string|null
     */
    public function getCreatedAtColumn()
    {
        return static::CREATED_AT;
    }

Did you mean to use a mutator?

public function getCreatedAtAttribute($date)
    {
        return 'test';
    }
Sinnbeck's avatar

@ThisIsPuzzle I assume this is an API that only you are using? If it is used by external people as well, then it is bad practice to not supply a timezone

ThisIsPuzzle's avatar

@Sinnbeck I was looking for changing the format date of the created_at returned by the model, but when i added the getCreatedAtColumn() { return 'test' } the format of the date in the API, created_at changed to Y-m-d H:i:s which was weird, since getCreatedAtColumn() was returning 'test' . And i found that weird. :D I was just curious why that happened.

ThisIsPuzzle's avatar

@Sinnbeck Yeah don't care about the timezone since the project it only uses one timezone, but will get more deep into how timezone works in laravel.

Sinnbeck's avatar

@ThisIsPuzzle my logic is that laravel cannot find the column and falls back to a call to now() or similar :)

Please or to participate in this conversation.