diff options
| author | Sam Light <samlight1994@gmail.com> | 2025-04-10 23:52:34 +0100 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2025-04-10 23:52:34 +0100 | 
| commit | 886b0d0a5fe9df7f92c9a91e961f58607febe2ea (patch) | |
| tree | 70fa68531908bf51d287cfe37650ea251369f979 | |
| parent | 39dd269b79a1d307c9823d46704a9280e52f6e2c (diff) | |
Created trait to add relationships to user model for auth logsv1.1.0
| -rw-r--r-- | src/HasAuthLogs.php | 41 | 
1 files changed, 41 insertions, 0 deletions
| diff --git a/src/HasAuthLogs.php b/src/HasAuthLogs.php new file mode 100644 index 0000000..543b580 --- /dev/null +++ b/src/HasAuthLogs.php @@ -0,0 +1,41 @@ +<?php + +namespace Lightscale\LaralightAuthLog; + +use Lightscale\LaralightAuthLog\Models\AuthLog; +use Lightscale\LaralightAuthLog\Enums\Status; + +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Casts\Attribute; + +use DateTimeInterface; + +trait HasAuthLogs +{ + +    public function authLogs(): HasMany +    { +        return $this->hasMany(AuthLog::class) +            ->orderBy('created_at', 'desc'); +    } + +    public function successfulAuthLogs(): HasMany +    { +        return $this->authLogs() +            ->where('status', Status::LoginSuccess); +    } + +    public function successfulAuthLog(): HasOne +    { +        return $this->successfulAuthLogs()->one(); +    } + +    public function lastLogin(): Attribute +    { +        return Attribute::get( +            fn(): ?DateTimeInterface => $this->successfulAuthLog?->created_at +        ); +    } + +} | 
