ldc-le-gaulois-pro/app/Models/User.php

85 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2022-08-31 09:18:53 +00:00
<?php
namespace App\Models;
2022-11-29 14:47:07 +01:00
use App\Lib\Helpers\ToolboxHelper;
2022-08-31 09:18:53 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
* @property integer $id Id.
* @property string $firstname Firstname.
* @property string $lastname Lastname.
* @property string $email Email.
* @property string $remember_token Remember_token.
* @property \Carbon\Carbon $last_connection Last_connection.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
2022-08-31 09:18:53 +00:00
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'firstname',
'lastname',
2024-07-20 22:31:13 +02:00
'email'
2022-08-31 09:18:53 +00:00
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
2024-07-20 22:31:13 +02:00
'remember_token'
2022-08-31 09:18:53 +00:00
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
2024-07-20 22:31:13 +02:00
'last_connection' => 'date',
2022-08-31 09:18:53 +00:00
];
2022-11-29 14:47:07 +01:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
2022-11-29 14:47:07 +01:00
{
// * EVENT CLOSURES.
static::creating(function (self $user) {
2024-02-05 09:33:08 +01:00
self::assertEmailIsUnique($user->email);
2022-11-29 14:47:07 +01:00
});
static::updating(function (self $user) {
2024-02-05 09:33:08 +01:00
self::assertEmailIsUnique($user->email, $user->id);
2022-11-29 14:47:07 +01:00
});
}
// * METHODS
/**
* Asserts using validation that the email is unique
*
* @param string $email
2022-11-29 14:47:07 +01:00
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If user email as already used.
*/
private static function assertEmailIsUnique(string $email, ?int $id = null): void
2022-11-29 14:47:07 +01:00
{
ToolboxHelper::assertFieldIsUnique((new self())->getTable(), 'email', $email, $id);
}
2022-08-31 09:18:53 +00:00
}