Added User Settings
This commit is contained in:
+29
-13
@@ -3,9 +3,11 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Http\Controllers\UserFlightController;
|
||||
use App\Settings\SettingsRegistry;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@@ -13,7 +15,7 @@ use App\Traits\HasAchievements;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'distance_unit'])]
|
||||
#[Fillable(['name', 'email', 'password', 'distance_unit', 'settings'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -21,24 +23,38 @@ class User extends Authenticatable
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, HasAchievements, HasApiTokens, HasRoles;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'settings' => 'array',
|
||||
];
|
||||
|
||||
protected $appends = ['resolved_settings'];
|
||||
|
||||
public function achievements(): HasMany
|
||||
{
|
||||
return $this->hasMany(UserAchievement::class);
|
||||
}
|
||||
|
||||
public function getSetting(string $key): mixed
|
||||
{
|
||||
$defaults = SettingsRegistry::defaults();
|
||||
return $this->settings[$key] ?? $defaults[$key] ?? null;
|
||||
}
|
||||
|
||||
public function updateSettings(array $values): void
|
||||
{
|
||||
$current = array_merge(SettingsRegistry::defaults(), $this->settings ?? []);
|
||||
$this->update(['settings' => array_merge($current, $values)]);
|
||||
}
|
||||
|
||||
protected function resolvedSettings(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => array_merge(SettingsRegistry::defaults(), $this->settings ?? [])
|
||||
);
|
||||
}
|
||||
|
||||
public function unlockedAchievements(): HasMany
|
||||
{
|
||||
return $this->achievements()
|
||||
|
||||
Reference in New Issue
Block a user