mardi 22 septembre 2015

Converting datetime data on retrieval with user's timezone using date mutators in Laravel 5

I'm saving all datetime data (created_at, updated_at and a custom datetime column: appointment_at) in UTC format.

I would like to retrieve all these dates, and convert them to the user's timezone for display.

I've added these functions to my model thinking this would retrieve the datetime in the user's timezone:

public function getCreatedAtAttribute($value)
{
    return $created_at->timezone(Auth::user()->timezone);
}

public function getUpdatedAtAttribute($value)
{
    return $updated_at->timezone(Auth::user()->timezone);
}

public function getAppointmentAtAttribute($value)
{
    return $appointment_at->timezone(Auth::user()->timezone);
}

But I get the following error:

Call to a member function timezone() on a non-object

I'm guessing there's an error in my syntax. Am I missing something? Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire