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

zelaza's avatar

Use created_at in accessor in Laravel 5.3?

Hi. I'm trying to use created_at in a model accessor method in Laravel 5.3, and I can't get it to work. Basically I am trying to create an appended property called 'age_days' using an accessor method called 'getAgeDaysAttribute()' that returns the age of the model in days since the 'created_at' date. But when I try to use 'created_at' in an accessor it is always null. Here is an example of the code I have tried.

   /*
     * Pseudo-fields that get added to the models when the models are exported to json.
     */
    protected $appends = ['age_days'];


    /*
     * Returns age of model in days since created_at.
     */
    public function getAgeDaysAttribute() {

        return $this->created_at->diffInDays();
        // return Carbon::createFromFormat('Y-m-d H:i:s', $this->created_at)->diffInDays();

    }

Oddly, the code above works for date fields OTHER THAN created_at. For example, the following code works:

   /*
     * Pseudo-fields that get added to the models when the models are exported to json.
     */
    protected $appends = ['age_days'];


    /*
     * Returns age of model in days since created_at.
     */
    public function getAgeDaysAttribute() {

        // note: using 'received_at' field rather than 'created_at' - this works
        return Carbon::createFromFormat('Y-m-d H:i:s', $this->received_at)->diffInDays();

    }
0 likes
7 replies
Snapey's avatar

is it the same if you use $this->attributes['created_at'] ?

zelaza's avatar

Thanks for responding. Yes, I tried $this->attributes['created_at'] and it failed (it was NULL). In fact even all of $this->attributes appeared NULL...

It's as if $this->created_at doesn't even exist, but it does (I can see it in the DB)...

sherwinmdev's avatar

@zelaza i checked my accessors and i use $this->attributes['column_name']

so try

return $this->attributes['created_at']->diffInDays();
zelaza's avatar

Thanks, but when I do that I get:

FatalErrorException in helpers.php line 519:

Method Illuminate\Database\Eloquent\Collection::__toString() must not throw an exception, caught ErrorException: Undefined index: created_at

zelaza's avatar

By the way, I'm using Laravel Spark - I don't think that should matter, but I am...

Farhan's avatar

@Zelaza spark is not matter. Try it with date matutor approach:

protected $dates = ['created_at'];
zelaza's avatar

As far as I can tell 'created_at' is not available in an accessor.

When I do this:

    // pseudo-fields appended to models when exported to json
    protected $appends = ['age_days'];

    // return age of model in days since 'created_at'
    public function getAgeDaysAttribute() {

        Log::info("\n\n****************************************");
        Log::info('$this->attributes: ' . var_export($this->attributes, true));

        $arrayDates = $this->getDates();
        Log::info("\n\n****************************************");
        Log::info('$arrayDates: ' . var_export($arrayDates, true));

    }

I get this:

[2017-02-04 07:43:45] production.INFO: 

****************************************  
[2017-02-04 07:43:45] production.INFO: $this->attributes: array (
  'state' => 'Open',
  'conversions' => 0,
  'conversion_value' => '0.0000',
  'conversion_currency' => '',
  'received_at' => '2017-02-04 07:34:19',
  'received_at_date' => '2017-02-04',
)  
[2017-02-04 07:43:45] production.INFO: 

****************************************  
[2017-02-04 07:43:45] production.INFO: $arrayDates: array (
  0 => 'created_at',
  1 => 'updated_at',
)  

It seems like 'created_at' is not an attribute of the model and there is no way to "get it" in an accessor...?

Please or to participate in this conversation.