Archived
feat: rework logging
This commit is contained in:
1 parent
87bdbeee05
commit
2d5991988d
15 files changed
+204
-95
No files matched your search
+18
-10
@@ -2,18 +2,20 @@
|
||||
|
||||
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;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
/**
|
||||
* @mixin IdeHelperUser
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The model's default values for attributes.
|
||||
@@ -62,27 +64,33 @@ class User extends Authenticatable
|
||||
'password' => 'hashed',
|
||||
];
|
||||
|
||||
public function logs(): hasMany
|
||||
public function logAction(): HasMany
|
||||
{
|
||||
return $this->hasMany(Log::class);
|
||||
}
|
||||
|
||||
public function fullName(): string
|
||||
public function logs(): MorphOne
|
||||
{
|
||||
return "{$this->firstname} {$this->lastname}";
|
||||
return $this->morphOne(Log::class, 'loggable');
|
||||
}
|
||||
|
||||
public function getFullNameAttribute(): string
|
||||
{
|
||||
return "$this->firstname $this->lastname";
|
||||
}
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// These will only ever get created by the api/bot.
|
||||
// If we want to add something like by whom later on,
|
||||
// we can just append it to the content e.g auth()?->user()?->id ?? 'API'.
|
||||
self::created(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => $model->id,
|
||||
'content' => 'User created.',
|
||||
'user_id' => auth()?->user()?->id ?? 1,
|
||||
'loggable_type' => self::class,
|
||||
'loggable_id' => $model->id,
|
||||
'action' => LogAction::CREATE,
|
||||
'content' => $model->full_name,
|
||||
'ip' => request()->ip(),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user