ctrlpanel/app/Models/Settings.php

52 lines
1 KiB
PHP
Raw Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2021-11-07 01:13:36 +00:00
use Illuminate\Support\Facades\Cache;
2021-06-05 09:26:32 +00:00
class Settings extends Model
2021-06-05 09:26:32 +00:00
{
use HasFactory;
2022-01-05 09:32:17 +00:00
protected $table = 'settings';
public const CACHE_TAG = 'setting';
2021-11-07 01:13:36 +00:00
2021-06-05 09:26:32 +00:00
public $primaryKey = 'key';
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'key',
'value',
'type',
];
2021-11-07 01:13:36 +00:00
public static function boot()
{
parent::boot();
static::updated(function (Settings $settings) {
Cache::forget(self::CACHE_TAG.':'.$settings->key);
2021-11-07 01:13:36 +00:00
});
}
2021-06-05 09:26:32 +00:00
/**
* @param string $key
2021-06-05 09:26:32 +00:00
* @param $default
* @return mixed
*/
2021-11-07 01:13:36 +00:00
public static function getValueByKey(string $key, $default = null)
2021-06-05 09:26:32 +00:00
{
return Cache::rememberForever(self::CACHE_TAG.':'.$key, function () use ($default, $key) {
$settings = self::find($key);
return $settings ? $settings->value : $default;
2021-11-07 01:13:36 +00:00
});
2021-06-05 09:26:32 +00:00
}
}