store/app/Models/UserProfile.php

56 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Gender;
use App\Models\Concerns\InteractsWithActivityLog;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
#[Guarded(['id'])]
#[Appends(['birth_date_formatted', 'birth_date_input', 'gender_label'])]
class UserProfile extends Model
{
use InteractsWithActivityLog;
use SoftDeletes;
protected function casts(): array
{
return [
'gender' => Gender::class,
'birth_date' => 'date',
];
}
protected function birthDateFormatted(): Attribute
{
return Attribute::make(
get: fn () => Carbon::parse($this->birth_date)->format('l, d F Y'),
);
}
protected function birthDateInput(): Attribute
{
return Attribute::make(
get: fn () => $this->birth_date?->format('Y-m-d'),
);
}
protected function genderLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->gender?->label(),
);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}