Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Nova: DateTime in Model Timezone

I want Laravel Nova to handle a DateTime field (stored in the database in UTC) in a custom timezone based on the Model and not in the User's timezone it does by default:

https://nova.laravel.com/docs/3.0/resources/date-fields.html#timezones

The field is for event start times at various locations across timezones throughout the United States. It does not make sense to display them in the user's timezone since they will be sorted with a location filter or viewed in respect to the Location model.

Does anyone have a solution for how to handle this use case?

like image 211
Drew Roberts Avatar asked Nov 07 '22 04:11

Drew Roberts


1 Answers

Try this instead, create new column in users table called timezone.

Schema::table('users', function (Blueprint $table) {
        $table->string('timezone')->nullable();
    });

In User resource create timezone field like this

Timezone::make('Timezone')
                ->default('yourtimezone'),

then in novaServiceProvider add following in boot()

Nova::userTimezone(function (Request $request) {
        return $request->user()->timezone;
    });

this allows you to have control over user timezones. Once user is logged in with the specific timezone he/she will be able to create events at various locations across timezones.

like image 189
A.Z Avatar answered Nov 27 '22 13:11

A.Z