ldc-com-app-bo/app/Models/User.php

100 lines
2 KiB
PHP
Raw Permalink Normal View History

2023-03-14 14:57:03 +01:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
2023-07-13 08:34:54 +00:00
use App\Enums\UserRolesEnum;
2023-03-14 14:57:03 +01:00
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'firstname',
'lastname',
'email',
2023-03-27 12:17:31 +02:00
'email_verified_at',
2023-03-14 14:57:03 +01:00
'password',
2023-03-27 12:17:31 +02:00
'role'
2023-03-14 14:57:03 +01:00
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
2023-07-13 08:34:54 +00:00
/**
* cast enum
* @var string[]
*/
protected $enumCasts = [
'role' => UserRolesEnum::class
];
2023-03-14 14:57:03 +01:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
2023-07-25 12:55:22 +02:00
static::creating(function (self $model) {
static::updatePassword($model);
2023-03-14 14:57:03 +01:00
});
2023-07-25 12:55:22 +02:00
static::updating(function (self $model) {
static::updatePassword($model);
2023-03-14 14:57:03 +01:00
});
}
2023-07-25 12:55:22 +02:00
// * METHODS
/**
* Update password.
*
* @param self $model
* @return void
*/
private static function updatePassword(self $model): void
{
if (Hash::needsRehash($model->password)) {
$model->password = Hash::make($model->password);
}
}
2023-07-21 14:51:02 +02:00
// * RELATIONSHIPS
/**
* MorphToMany directories relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function directories()
{
return $this->morphToMany(Directory::class, 'directorables');
}
2023-03-14 14:57:03 +01:00
}