70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class UserProfile extends Model
|
|
{
|
|
protected $table = 'user_profiles';
|
|
protected $primaryKey = 'user_id';
|
|
public $incrementing = false;
|
|
protected $keyType = 'int';
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'about',
|
|
'signature',
|
|
'description',
|
|
'avatar',
|
|
'avatar_hash',
|
|
'avatar_mime',
|
|
'avatar_updated_at',
|
|
'cover_image',
|
|
'country',
|
|
'country_code',
|
|
'language',
|
|
'birthdate',
|
|
'gender',
|
|
'website',
|
|
];
|
|
|
|
protected $casts = [
|
|
'birthdate' => 'date',
|
|
'avatar_updated_at' => 'datetime',
|
|
];
|
|
|
|
public $timestamps = true;
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* Return a public URL for the avatar when stored on the `public` disk under `avatars/`.
|
|
*/
|
|
public function getAvatarUrlAttribute(): ?string
|
|
{
|
|
if (empty($this->avatar)) {
|
|
return null;
|
|
}
|
|
|
|
// If the stored value already looks like a full URL, return it.
|
|
if (preg_match('#^https?://#i', $this->avatar)) {
|
|
return $this->avatar;
|
|
}
|
|
|
|
// Prefer `public` disk and avatars folder.
|
|
$path = 'avatars/' . ltrim($this->avatar, '/');
|
|
if (Storage::disk('public')->exists($path)) {
|
|
return Storage::disk('public')->url($path);
|
|
}
|
|
|
|
// Fallback: return null if not found
|
|
return null;
|
|
}
|
|
}
|