feat: next level xp

This commit is contained in:
Leon 2026-01-09 15:27:14 +01:00
parent c3246a0ec1
commit eda8e02a47
1 changed files with 16 additions and 1 deletions

View File

@ -52,6 +52,15 @@ class User extends Authenticatable implements HasName, FilamentUser
'level_id' 'level_id'
]; ];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'next_level_xp',
];
/** /**
* The attributes that should be hidden for serialization. * The attributes that should be hidden for serialization.
* *
@ -93,12 +102,18 @@ class User extends Authenticatable implements HasName, FilamentUser
$level = Level::where('xpThreshold', '<=', $this->xp) $level = Level::where('xpThreshold', '<=', $this->xp)
->orderBy('xpThreshold', 'desc') ->orderBy('xpThreshold', 'desc')
->first(); ->first();
if ($level && $this->level_id !== $level->id) { if ($level && $this->level_id !== $level->id) {
$this->level_id = $level->id; $this->level_id = $level->id;
$this->save(); $this->save();
} }
} }
public function getNextLevelXpAttribute(): ?int
{
return Level::where('xpThreshold', '>', $this->xp)
->orderBy('xpThreshold', 'asc')
->value('xpThreshold');
}
public function getFilamentName(): string public function getFilamentName(): string
{ {
return $this->username; return $this->username;