Skip to content

Commit ebaf70f

Browse files
authored
Merge pull request #10 from gdebrauwer/delete-old-codes-query-fix
Fix and simplify query to delete old codes
2 parents b65b3bd + f39839e commit ebaf70f

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

src/Models/VerificationCode.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ public static function boot()
5959
static::created(function ($verificationCode) {
6060
$maxCodes = config('verification-code.max_per_verifiable', 1);
6161

62-
if ($maxCodes !== null) {
63-
VerificationCode::exceptRecentFor($verificationCode->verifiable, $maxCodes)->delete();
62+
if ($maxCodes === null) {
63+
return;
6464
}
65+
66+
VerificationCode::for($verificationCode->verifiable)
67+
->orderByDesc('expires_at')
68+
->orderByDesc('id')
69+
->skip($maxCodes)
70+
->take(PHP_INT_MAX)
71+
->delete();
6572
});
6673
}
6774

@@ -104,28 +111,4 @@ public function scopeFor($query, string $verifiable)
104111
{
105112
return $query->where('verifiable', $verifiable);
106113
}
107-
108-
/**
109-
* Scope a query to only include verification codes from the provided verifiable.
110-
*
111-
* @param \Illuminate\Database\Eloquent\Builder $query
112-
* @param string $verifiable
113-
* @param int $amount
114-
*
115-
* @return \Illuminate\Database\Eloquent\Builder
116-
*/
117-
public function scopeExceptRecentFor($query, string $verifiable, int $amount)
118-
{
119-
return $query
120-
->for($verifiable)
121-
->whereNotIn(
122-
'id',
123-
static::query()
124-
->select('id')
125-
->for($verifiable)
126-
->orderByDesc('expires_at')
127-
->orderByDesc('id')
128-
->limit($amount)
129-
);
130-
}
131114
}

0 commit comments

Comments
 (0)