Adam Anderly

Husband, Father, Developer

  • About
  • Blog
  • Archives
  • Plugins
    • WooCommerce MailChimp
    • Wistia Responsive
  • Contact

Connect

  • 
  • 
  • 
  • 

Copyright © 2019
Adam Anderly ยท Log in

Laravel User Timezone Aware Trait

December 21, 2017 By Adam Leave a Comment

Next up in the series on useful Laravel model traits is the UserTimezoneAware Trait.

This trait is useful when you have dates on models that you need to display in the user’s timezone.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// app\Traits\UserTimezoneAware.php
namespace App\Traits;
 
trait UserTimezoneAware
{
    
    /**
     * The attribute name containing the timezone (defaults to "timezone").
     *
     * @var string
     */
    public $timezoneAttribute = 'timezone';
    
    /**
     * Return the passed date in the user's timezone (or default to the app timezone)
     *
     * @return string
     */
    public function getDateToUserTimezone($date, $timezone = null)
    {
        if ($timezone == null) {
            if (Auth::check()) {
                $timezone = Auth::user()->timezone;
            } else {
                $timezone = Config::get('app.timezone');
            }
        }
        $datetime = new DateTime($date);
        $datetime->setTimezone(new datetimezone($timezone));
        return $datetime->format('c');
    }
 
}

It’s a simple trait you can drop onto one of your eloquent models to add timezone support like so:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// app\Models\User.php
namespace App\Models;
 
use App\Traits\UserTimezoneAware
 
class User
{
    use UserTimezoneAware;
 
    public function getLastLoginAttribute()
    {
        return $this->getDateToUserTimezone($this->attributes['last_login']);
    }
}

Now when you access the $user->lastLogin attribute anywhere in your app, you’ll get the date in the user’s timezone.

The UserTimezoneAware trait expects the model to have a timezone attribute. If your column/attribute is named something other than timezone simply override the $timezoneAttribute property on the UserTimezoneAware trait.

PHP
1
2
3
4
5
6
7
8
9
10
namespace App\Models;
 
use App\Traits\UserTimezoneAware
class User
{
    use UserTimezoneAware;
 
    public $timezoneAttribute= 'time_zone';
}

Enjoy!

Share this:

  • Twitter
  • Google
  • LinkedIn
  • Email
  • Print
  • More
  • Reddit

Filed Under: Laravel, Laravel Traits, PHP Tagged With: Laravel, Laravel Traits, PHP

Useful Laravel Model Traits: Gravatar

September 9, 2016 By Adam Leave a Comment

Starting a little series here on useful Laravel model traits.

This first one I use quite often.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// app\Traits\HasGravatar.php
namespace App\Traits;
 
trait HasGravatar
{
    
    /**
     * The attribute name containing the email address.
     *
     * @var string
     */
    public $gravatarEmail = 'email';
    
    /**
     * Get the model's gravatar
     *
     * @return string
     */
    public function getGravatarAttribute()
    {
        $hash = md5(strtolower(trim($this->attributes[$this->gravatarEmail])));
        return "https://www.gravatar.com/avatar/$hash";
    }
 
}

It’s a simple trait you can drop onto one of your eloquent models to add gravatar support like so:

PHP
1
2
3
4
5
6
7
8
9
// app\Models\User.php
namespace App\Models;
 
use App\Traits\HasGravatar
 
class User
{
    use HasGravatar;
}

Now you can do something like this in your blade views:

PHP
1
<img src="{{ Auth::user()->gravatar }}" />

And you’ll get the gravatar.com URL you’re expecting. No need to create a gravatar column, variable, method, or anything else.

The HasGravatar Trait expects the model to have an email attribute. If your column/attribute is named something other than email simply override the $gravatarEmail property on the HasGravatar trait.

PHP
1
2
3
4
5
6
7
8
9
10
namespace App\Models;
 
use App\Traits\HasGravatar
 
class User
{
    use HasGravatar;
 
    public $gravatarEmail = 'email_address';
}

Enjoy!

Share this:

  • Twitter
  • Google
  • LinkedIn
  • Email
  • Print
  • More
  • Reddit

Filed Under: Laravel, Laravel Traits Tagged With: Laravel, Laravel Traits

RSS

RSS Feed

Subscribe

Enter your email address to subscribe and receive new posts by email.

Find It Here

Top Posts

  • Create your own branded url-shortener in under 10 minutes using ASP.NET MVC 2
  • To round 'up', or to round 'down': that is the question
  • Silverlight Midpoint Rounding Solution
  • Recreating the MobileMe Gallery in Silverlight - Part 1 *Updated*
  • Using SimpleMembership in MVC3
  • Why Microsoft Developers Should Care About Node.js
  • Laravel Transformable - An Eloquent Model Trait for Consumable Models
  • Laravel User Timezone Aware Trait
  • ASP.NET MVC, Dependency Injection and The Bliss of Truly Decoupled Applications
  • SimpleMembership.Mvc3 NuGet Package

Recent Posts

  • Laravel User Timezone Aware Trait
  • Useful Laravel Model Traits: Gravatar
  • Laravel Transformable – An Eloquent Model Trait for Consumable Models
  • Gravity Forms + Microsoft Dynamics CRM
  • DNS from the command line: dnsimple-cli for Node.js

Categories

  • .NET (11)
  • ASP.NET MVC (7)
  • Dependency Injection (3)
  • Laravel (3)
  • Laravel Traits (2)
  • Node.js (2)
  • NuGet (5)
  • PHP (2)
  • REST (4)
  • Silverlight (5)
  • Uncategorized (3)
  • WordPress (7)
  • WordPress Plugins (7)

Tags

.NET APIs ASP.NET MVC Azure cli CRM Dependency Injection dns dnsimple Dynamics Dynamics CRM Expansive Gravity Forms iPhone Laravel Laravel Traits MailChimp Microsoft Dynamics CRM MobileMe Node.js NuGet PHP REST Rounding Silverlight SimpleMembership sublime-text-2 VS2010 Wistia WooCommerce WordPress WordPress Plugins
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.