V
V
Vanya Huk2018-04-17 18:26:26
Laravel
Vanya Huk, 2018-04-17 18:26:26

How to return a formatted date in Laravel orm?

I make an ajax request, and I want to immediately return the formatted date,
in the blade template engine it works like this:

<date>{$item -> created_at -> diffForHumans()}</date>

but i need ajax request to return this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-04-17
@vanyahuk

protected $dates = [
    'created_at',
];
protected $appends = [
    'created_time',
];
public function getCreatedTimeAttribute()
{
    return $this->created_at->diffForHumans();
}

After that, your object will have $item->created_time available; and the JSON array will also have such a field and you will see it in the array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question