/
sebero
/
lk
Обзор
Документация
Войти
/
sebero
/
lk
Код
Пакеты
0
Релизы
0
Аналитика
master
app/Models/SmsCode.php
111 строк
2 KB
Молодцов Андрей Сергеевич
first_commit
20 фев 2026, 13:08
20 фев 2026, 13:08
f6c0eb9
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class SmsCode extends Model { protected $fillable = [ 'phone', 'code', 'company_id', 'company_data', 'max_user_id', 'resend_count', 'attempts', 'expires_at', 'verified_at', ]; protected $casts = [ 'company_data' => 'array', 'expires_at' => 'datetime', 'verified_at' => 'datetime', 'max_user_id' => 'integer', 'resend_count' => 'integer', ]; public function isExpired(): bool { return $this->expires_at->isPast(); } public function hasExceededAttempts(int $maxAttempts = 3): bool { return $this->attempts >= $maxAttempts; } public function incrementAttempts(): void { $this->increment('attempts'); } public function markAsVerified(): void { $this->update([ 'verified_at' => now() ]); } public static function findActive(string $phone, string $code): ?self { return self::where('phone', $phone) ->where('code', $code) ->where('expires_at', '>', now()) ->whereNull('verified_at') ->first(); } public static function cleanExpired(): int { return self::where('expires_at', '<', now()->subHours(24))->delete(); } public function incrementResendCount(): void { $this->increment('resend_count'); } public function hasExceededResends(int $maxResends = 3): bool { return $this->resend_count >= $maxResends; } public static function findLastByPhone(string $phone): ?self { return self::where('phone', $phone) ->whereNull('verified_at') ->orderBy('created_at', 'desc') ->first(); } public static function findByMaxUserId(int $maxUserId): ?self { return self::where('max_user_id', $maxUserId) ->whereNull('verified_at') ->orderBy('created_at', 'desc') ->first(); } }