lundi 20 janvier 2020

Override created_at value from Eloquent models, without disabling timestamps

In Laravel, is it possible to override the value for created_at values for a given Eloquent model?

The use case, is that I have the User model with a timezone column (e.g. America/Chicago). The app stores date values in UTC and for one user I'd like to transform the date to the timezone related.

I tried to use getCreatedAtAttribute but it doesn't work.

What I'm looking for is something like this.

public function getCreatedAtAttribute($value)
{
   $user = auth()->user();
   if ($user && $user->timezone) {
      $timezone = $user->timezone; // This value can be "America\Chicago"
   } else {
      $timezone = \Illuminate\Support\Facades\Config::get('app.timezone');
   }

   $datetime = new DateTime($date);
   $datetime->setTimezone(new datetimezone($timezone));

   return $datetime;
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Rb9hqF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire