This repository has been archived on 2024-05-20. You can view files and clone it, but cannot push or open issues or pull requests.

98 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\LogAction;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
* @mixin IdeHelperUser
*/
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'is_admin' => false,
'status' => true,
];
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'firstname',
'lastname',
'email',
'uuid',
'profile',
'status',
'is_admin',
'password',
];
/**
* 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 = [
'is_admin' => 'boolean',
'status' => 'boolean',
'password' => 'hashed',
];
public function logAction(): HasMany
{
return $this->hasMany(Log::class);
}
public function logs(): MorphOne
{
return $this->morphOne(Log::class, 'loggable');
}
public function getFullNameAttribute(): string
{
return "$this->firstname $this->lastname";
}
public static function boot(): void
{
parent::boot();
self::created(function ($model) {
Log::create([
'user_id' => auth()?->user()?->id ?? 1,
'loggable_type' => self::class,
'loggable_id' => $model->id,
'action' => LogAction::CREATE,
'content' => $model->full_name,
'ip' => request()->ip(),
]);
});
}
}