summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2025-04-10 23:52:34 +0100
committerSam Light <samlight1994@gmail.com>2025-04-10 23:52:34 +0100
commit886b0d0a5fe9df7f92c9a91e961f58607febe2ea (patch)
tree70fa68531908bf51d287cfe37650ea251369f979 /src
parent39dd269b79a1d307c9823d46704a9280e52f6e2c (diff)
Created trait to add relationships to user model for auth logsHEADv1.1.0master
Diffstat (limited to 'src')
-rw-r--r--src/HasAuthLogs.php41
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
+ );
+ }
+
+}