@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Support\SiteInfo;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Key/value store for editable site settings (e.g. contact details).
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $key
|
||||
* @property array<string, mixed> $value
|
||||
*/
|
||||
#[Fillable(['key', 'value'])]
|
||||
class SiteSetting extends Model
|
||||
{
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(fn () => SiteInfo::forget());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'value' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $default
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function get(string $key, array $default = []): array
|
||||
{
|
||||
return static::query()->where('key', $key)->first()?->value ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $value
|
||||
*/
|
||||
public static function put(string $key, array $value): void
|
||||
{
|
||||
static::query()->updateOrCreate(['key' => $key], ['value' => $value]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user