50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Token extends Model
|
|
{
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = ['user_id', 'token', 'type', 'created_at'];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public static function existsToken(User $user, string $type)
|
|
{
|
|
$userId = $user->id;
|
|
|
|
$token = self::where('user_id', $userId)
|
|
->where('type', 'email_verification')
|
|
->where('created_at', '>=', Carbon::now()->subDay())
|
|
->first();
|
|
|
|
return $token;
|
|
}
|
|
public static function existsTokenByToken(string $token, string $type)
|
|
{
|
|
$token = self::where('token', $token)
|
|
->where('type', $type)
|
|
->where('created_at', '>=', Carbon::now()->subDay())
|
|
->first();
|
|
|
|
return $token;
|
|
}
|
|
public static function createToken(User $user, string $type)
|
|
{
|
|
return self::create([
|
|
'user_id' => $user->id,
|
|
'token' => Str::random(64),
|
|
'type' => $type,
|
|
'created_at' => now(),
|
|
]);
|
|
}
|
|
}
|