summaryrefslogtreecommitdiff
path: root/src/HasAuthLogs.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/HasAuthLogs.php')
-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
+ );
+ }
+
+}