ldc-boutique-cnc/app/Models/User.php

92 lines
1.9 KiB
PHP
Raw Normal View History

2020-10-30 17:26:00 +01:00
<?php
namespace App\Models;
use App\Enums\UserRole;
2020-10-30 17:26:00 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2022-09-30 15:11:52 +02:00
use Illuminate\Support\Facades\Hash;
use Kwaadpepper\Enum\Traits\CastsEnums;
2020-10-30 17:26:00 +01:00
class User extends Authenticatable
{
use CastsEnums;
2022-08-25 12:26:43 +02:00
use HasFactory;
use Notifiable;
2020-10-30 17:26:00 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
2020-10-30 17:26:00 +01:00
'email',
'password',
'date_of_birth',
'role'
2020-10-30 17:26:00 +01:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
2020-11-17 13:41:31 +01:00
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'role' => UserRole::class,
];
2022-09-30 15:11:52 +02:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (User $user) {
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
});
static::updating(function (User $user) {
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
});
}
// * RELATIONSHIPS
2022-09-03 16:56:16 +02:00
/**
* hasMany order relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2022-08-25 12:26:43 +02:00
public function orders()
{
2023-03-01 11:02:42 +01:00
return $this->hasMany('App\Models\Shop\Order', 'user_id');
2020-11-17 13:41:31 +01:00
}
2020-10-30 17:26:00 +01:00
}