Files
laravel-nuxt-ui-inertia-tem…/app/Models/User.php

99 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'username',
'first_name',
'last_name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The accessors to append to the model's array form.
*
* @var list<string>
*/
protected $appends = [
'full_name',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
/**
* Only send verification notification when the feature is enabled.
*/
public function sendEmailVerificationNotification(): void
{
if (config('auth-ui.features.email_verification')) {
parent::sendEmailVerificationNotification();
}
}
/**
* Check if the user has a password set (i.e. not a social-only user).
*/
public function hasPassword(): bool
{
return $this->password !== null;
}
/**
* Get the user's social accounts.
*/
public function socialAccounts(): HasMany
{
return $this->hasMany(SocialAccount::class);
}
/**
* Get the user's full name.
*/
protected function fullName(): Attribute
{
return Attribute::make(
get: fn () => trim("{$this->first_name} {$this->last_name}"),
);
}
}