e6f38d9481
* docs: remove requirement for setting APP_KEY manually * optimize date picker * clean up modals * spot light working * reorganization * add lazy load * wip * remove filament * styling
78 lines
2.0 KiB
PHP
78 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
trait HasProfilePhoto
|
|
{
|
|
/**
|
|
* Update the user's profile photo.
|
|
*
|
|
* @param string $storagePath
|
|
* @return void
|
|
*/
|
|
public function updateProfilePhoto(UploadedFile $photo, $storagePath = 'profile-photos')
|
|
{
|
|
tap($this->profile_photo_path, function ($previous) use ($photo, $storagePath) {
|
|
$this->forceFill([
|
|
'profile_photo_path' => $photo->storePublicly(
|
|
$storagePath, ['disk' => 'public']
|
|
),
|
|
])->save();
|
|
|
|
if ($previous) {
|
|
Storage::disk('public')->delete($previous);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete the user's profile photo.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function deleteProfilePhoto()
|
|
{
|
|
if (is_null($this->profile_photo_path)) {
|
|
return;
|
|
}
|
|
|
|
Storage::disk('public')->delete($this->profile_photo_path);
|
|
|
|
$this->forceFill([
|
|
'profile_photo_path' => null,
|
|
])->save();
|
|
}
|
|
|
|
/**
|
|
* Get the URL to the user's profile photo.
|
|
*/
|
|
protected function profilePhotoUrl(): Attribute
|
|
{
|
|
return Attribute::get(function (): string {
|
|
return $this->profile_photo_path
|
|
? Storage::disk('public')->url($this->profile_photo_path)
|
|
: $this->defaultProfilePhotoUrl();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the default profile photo URL if no profile photo has been uploaded.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function defaultProfilePhotoUrl()
|
|
{
|
|
$name = trim(collect(explode(' ', $this->name))->map(function ($segment) {
|
|
return mb_substr($segment, 0, 1);
|
|
})->join(' '));
|
|
|
|
return 'https://ui-avatars.com/api/?name='.urlencode($name).'&color=7F9CF5&background=EBF4FF';
|
|
}
|
|
}
|