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 Configuration extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
2021-11-07 01:13:36 +00:00
|
|
|
public const CACHE_TAG = 'configuration';
|
|
|
|
|
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 (Configuration $configuration) {
|
|
|
|
Cache::forget(self::CACHE_TAG .':'. $configuration->key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-05 09:26:32 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @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
|
|
|
{
|
2021-11-07 01:13:36 +00:00
|
|
|
return Cache::rememberForever(self::CACHE_TAG .':'. $key, function () use ($default, $key) {
|
|
|
|
$configuration = self::find($key);
|
|
|
|
return $configuration ? $configuration->value : $default;
|
|
|
|
});
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
}
|