Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to add a custom attribute to model in laravel

Tags:

php

laravel

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.

like image 919
Neha Avatar asked Aug 09 '18 08:08

Neha


1 Answers

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.

like image 67
SteD Avatar answered Sep 28 '22 00:09

SteD