7

I need to add an extra attribute to my laravel model without using get Attribute. i want to get created_at attribute in both default and custom format.

default : created_at : 2018-07-25 15:38:56

second: 10 Days Ago 

if i use getCreatedAtAttribute() it changes the default attribute.

1
  • 2
    Just change the name of the custom attribute. Use something like getDaysCreatedAttribute() or similar. Aug 9, 2018 at 8:33

1 Answer 1

10

In your model, add this:

protected $appends = ['readable_created_at'];

public function getReadableCreatedAtAttribute()
{
    return $this->created_at; //or however you want to manipulate it
}

After which, you can access it like normal, e.g. $user->readable_created_at.

Read more at the Laravel Doc here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.