Search code examples
phplaravellaravel-nova

Laravel nova diffForHumans DateTime


I have field last_active on users, I want display time with diffForHumans or time_from_now from Moment.js. How I can do it? Now I just use:

DateTime::make('Activiy', 'last_active')
            ->exceptOnForms(),

When I use:

DateTime::make('Activiy', 'last_active')->diffForHumans()
            ->exceptOnForms(),

I get undifined method diffForHumans.


Solution

  • To my knowledge at the moment DateTime only supports format.

    Since you want only to display, you can try Text field & display the humanise value. If your column might be null be sure to check for that otherwise you will get an error.

    Text::make('Activiy', 'last_active')
        ->displayUsing(function($lastActive) {
            if ($lastActive === null) {
                return null;
            }
            return $lastActive->diffForHumans();
        })
        ->exceptOnForms(),