77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Gender;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Appends(['formatted_birth_date', 'gender_label', 'formatted_phone_number'])]
|
|
#[Guarded(['id'])]
|
|
class UserProfile extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'gender' => Gender::class,
|
|
'birth_date' => 'date:Y-m-d',
|
|
];
|
|
}
|
|
|
|
protected function formattedBirthDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->birth_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function genderLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->gender?->label(),
|
|
);
|
|
}
|
|
|
|
protected function formattedPhoneNumber(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->phone_number
|
|
? substr($this->phone_number, 0, 4) . ' ' . substr($this->phone_number, 4, 4) . ' ' . substr($this->phone_number, 8)
|
|
: null,
|
|
set: fn (?string $value) => $value ? preg_replace('/\s/', '', $value) : null,
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function female(Builder $query): void
|
|
{
|
|
$query->where('gender', Gender::FEMALE);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function hasPhoneNumber(Builder $query): void
|
|
{
|
|
$query->whereNotNull('phone_number');
|
|
}
|
|
|
|
#[Scope]
|
|
protected function male(Builder $query): void
|
|
{
|
|
$query->where('gender', Gender::MALE);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
}
|