1

I use Auth in all my models to convert datetimes to the user's timezone when retrieved. Now I ran into a situation where the user is not authenticated, and this conversion needs to be done in my model.

My goal would be to retrieve a default timezone from another table like so:

        $business = Business::where('id', '=', $id)->first(); // default timezone is in here: $business->timezone

Since I use \Auth::user()->timezone in all my model, is there a place where I should add the code above (I'll need to pass $id which is not available when user is authenticated) for the retrieval of the default timezone, so I can access $business->timezone globally, only when a user is not authenticated?

This is the code I currently have in one of my models.

/**
 * Set event_start attribute.
 */
public function setEventStartAttribute($event_start)
{
    $this->attributes['event_start'] = Carbon::createFromFormat('Y-m-d H:i', $event_start, \Auth::user()->timezone)->setTimezone('UTC');
}

/**
 * Set event_end attribute.
 */
public function setEventEndAttribute($event_end)
{
    $this->attributes['event_end'] = Carbon::createFromFormat('Y-m-d H:i', $event_end, \Auth::user()->timezone)->setTimezone('UTC');
}

/**
 * Get event_end attribute.
 */
public function getEventEndAttribute($value)
{
    $format = $this->getDateFormat();
    return  Carbon::createFromFormat($format, $value, 'UTC')->setTimezone( \Auth::user()->timezone);
}       

/**
 * Get event_end attribute.
 */
public function getEventEndAttribute($value)
{
    $format = $this->getDateFormat();
    return  Carbon::createFromFormat($format, $value, 'UTC')->setTimezone( \Auth::user()->timezone);
}   

I guess my code would then be changed to something like:

/**
 * Set event_start attribute.
 */
public function setEventStartAttribute($event_start)
{
    if (Auth::check()) {
        $this->attributes['event_start'] = Carbon::createFromFormat('Y-m-d H:i', $event_start, \Auth::user()->timezone)->setTimezone('UTC');
    }
    else {
        $this->attributes['event_start'] = Carbon::createFromFormat('Y-m-d H:i', $event_start, $business->timezone)->setTimezone('UTC');
    }
}   

Thanks

user3489502
  • 3,451
  • 9
  • 38
  • 66

2 Answers2

0

In this case, Cache is an option for default timezone. Then you create a class that will serve this information statically.

Create a class like:

public class MyTimezone {
    public static function getTimezone(){
        if(\Auth::check()) {
            $timezone = \Auth::user()->timezone;
        } else {
            $timezone = Cache::get('default_timezone');
            if(empty($timezone)) {
                //query business here
                $timezone = $business->timezone;
                Cache::put('default_timezone', $business->timezone); //cache here and "never" query again
            }
        }
        return $timezone;
    }
}

And use like this:

$this->attributes['event_start'] = Carbon::createFromFormat('Y-m-d H:i', $event_start, MyTimezone::getTimezone())->setTimezone('UTC');
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • Thanks I like the code you posted, but why Cache and not a custom helper? Sorry, just trying to understand – user3489502 May 17 '16 at 17:50
  • `MyTimezone` will act as a helper. You can call `MyTimezoneHelper`. The point is to Cache your `business->timezone` information to avoid multiple queries on your DB. If you don't care about it, just remove the cache part. – Felippe Duarte May 17 '16 at 17:54
  • Ok, so if understand, a helper function will get executed (query the database all the time), but a Cache would get executed once (only one call to database). Where would be the best place to create that class? – user3489502 May 17 '16 at 18:56
  • It's up to you. You can follow the guide from @sef4eg and use as Helper. – Felippe Duarte May 17 '16 at 19:29
  • Ok, good idea, I'll create an helper and use the Cache option. Thanks so much for your help – user3489502 May 17 '16 at 19:31
0

Create a custom helper and make it accessible from anywhere in your application, take a look at this post:

Best practices for custom helpers on Laravel 5

Community
  • 1
  • 1
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36
  • Thanks, I've seen some good examples in there. The query I would have in my custom helper would rely on an `{id}` from a specific route (`$business = Business::where('id', '=', $id)->first();`) . Is there a way to execute the customer helper for a specific route only? Thanks – user3489502 May 17 '16 at 18:25
  • Ok, found my answer. Guess I'll check which route I'm on (`if(Route::current()->getName() == 'myNonAuthRoute')`) before executing the helper. Thanks – user3489502 May 17 '16 at 19:46