2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
/**
|
|
|
|
* @property string $key
|
2018-09-03 12:41:49 +00:00
|
|
|
* @property mixed $value
|
2019-08-05 10:57:36 +00:00
|
|
|
*
|
2019-08-05 10:56:48 +00:00
|
|
|
* @method static self find(string $key)
|
|
|
|
* @method static self updateOrCreate(array $where, array $params)
|
2017-12-09 18:34:27 +00:00
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
class Setting extends Model
|
|
|
|
{
|
|
|
|
protected $primaryKey = 'key';
|
|
|
|
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
protected $guarded = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a setting value.
|
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @return mixed|string
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public static function get(string $key)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
if ($record = self::find($key)) {
|
|
|
|
return $record->value;
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
return null;
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a setting (no pun) value.
|
|
|
|
*
|
2020-09-06 21:20:42 +00:00
|
|
|
* @param string|array $key the key of the setting, or an associative array of settings,
|
|
|
|
* in which case $value will be discarded
|
2015-12-14 13:22:39 +00:00
|
|
|
* @param mixed $value
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public static function set($key, $value = null): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
if (is_array($key)) {
|
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
self::set($k, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::updateOrCreate(compact('key'), compact('value'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the setting value before saving into the database.
|
|
|
|
* This makes settings more flexible.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function setValueAttribute($value): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$this->attributes['value'] = serialize($value);
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:32:16 +00:00
|
|
|
/**
|
|
|
|
* Get the unserialized setting value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function getValueAttribute($value)
|
|
|
|
{
|
|
|
|
return unserialize($value);
|
|
|
|
}
|
|
|
|
}
|