This commit is contained in:
ct
2025-07-03 20:05:53 +08:00
parent 35c7c0bebd
commit 3d7b3c428b
6 changed files with 233 additions and 24 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ExportToken extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'token',
'is_premium',
'credits_reserved',
'expires_at',
'used_at',
];
protected $casts = [
'is_premium' => 'boolean',
'expires_at' => 'datetime',
'used_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isExpired(): bool
{
return $this->expires_at->isPast();
}
public function isUsed(): bool
{
return !is_null($this->used_at);
}
public function isValid(): bool
{
return !$this->isExpired() && !$this->isUsed();
}
public function markAsUsed(): void
{
$this->update(['used_at' => now()]);
}
public function scopeExpiredAndUnused($query)
{
return $query->where('expires_at', '<', now())
->whereNull('used_at');
}
public function scopeForUser($query, $userId)
{
return $query->where('user_id', $userId);
}
}