79 lines
1.6 KiB
PHP
79 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
/**
|
|
* @property string $device_fingerprint
|
|
* @property string|null $device_name
|
|
* @property string|null $platform android/ios
|
|
* @property string|null $os_version
|
|
* @property string|null $app_version
|
|
* @property bool $is_trusted
|
|
* @property string|null $push_token
|
|
*/
|
|
class UserDevice extends BaseModel
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'device_fingerprint',
|
|
'device_name',
|
|
'platform',
|
|
'os_version',
|
|
'app_version',
|
|
'is_trusted',
|
|
'push_token',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_trusted' => 'boolean',
|
|
'first_seen_at' => 'datetime',
|
|
'last_seen_at' => 'datetime',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'id',
|
|
'user_id',
|
|
'push_token',
|
|
];
|
|
|
|
// ── Relationships ──
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
// ── Scopes ──
|
|
|
|
public function scopeTrusted($query)
|
|
{
|
|
return $query->where('is_trusted', true);
|
|
}
|
|
|
|
public function scopeForFingerprint($query, string $fingerprint)
|
|
{
|
|
return $query->where('device_fingerprint', $fingerprint);
|
|
}
|
|
|
|
// ── Helpers ──
|
|
|
|
public function markSeen(): bool
|
|
{
|
|
return $this->update(['last_seen_at' => now()]);
|
|
}
|
|
|
|
public function trust(): bool
|
|
{
|
|
return $this->update(['is_trusted' => true]);
|
|
}
|
|
|
|
public function revoke(): bool
|
|
{
|
|
return $this->update(['is_trusted' => false]);
|
|
}
|
|
}
|