migrate template
This commit is contained in:
49
app/Models/SiteSetting.php
Normal file
49
app/Models/SiteSetting.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\SiteSettingFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class SiteSetting extends Model
|
||||
{
|
||||
/** @use HasFactory<SiteSettingFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $primaryKey = 'key';
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected $fillable = [
|
||||
'key',
|
||||
'value',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get a setting value by key.
|
||||
*/
|
||||
public static function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
return static::find($key)?->value ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a setting value by key.
|
||||
*/
|
||||
public static function set(string $key, ?string $value): void
|
||||
{
|
||||
static::updateOrCreate(['key' => $key], ['value' => $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings as a key-value collection.
|
||||
*/
|
||||
public static function allAsCollection(): Collection
|
||||
{
|
||||
return static::pluck('value', 'key');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user