Update Profile model, improve #381

This commit is contained in:
Daniel Supernault 2018-08-15 18:21:42 -06:00
parent df96b800c4
commit 2e8fb762ae
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -125,14 +125,16 @@ class Profile extends Model
public function avatar() public function avatar()
{ {
return $this->hasOne(Avatar::class); return $this->hasOne(Avatar::class)->withDefault([
'media_path' => 'public/avatars/default.png'
]);
} }
public function avatarUrl() public function avatarUrl()
{ {
$url = Cache::remember("avatar:{$this->id}", 1440, function() { $url = Cache::remember("avatar:{$this->id}", 1440, function() {
$path = $this->avatar->media_path ?? 'public/avatars/default.png'; $path = optional($this->avatar)->media_path;
$version = $this->avatar ? hash('sha1', $this->avatar->created_at) : ''; $version = hash('sha1', $this->avatar->created_at);
$path = "{$path}?v={$version}"; $path = "{$path}?v={$version}";
return url(Storage::url($path)); return url(Storage::url($path));
}); });