2017-12-10 11:21:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2022-03-14 23:45:18 +08:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-03-19 09:37:35 +08:00
|
|
|
/**
|
|
|
|
* @property string id
|
|
|
|
* @property string name
|
|
|
|
* @property string key
|
|
|
|
* @property string value
|
|
|
|
* @property string group
|
|
|
|
* @property string type
|
|
|
|
* @property string options
|
2022-03-14 23:45:18 +08:00
|
|
|
* @property int order
|
2018-03-19 09:37:35 +08:00
|
|
|
* @property string description
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Setting extends Model
|
2017-12-10 11:21:49 +08:00
|
|
|
{
|
|
|
|
public $table = 'settings';
|
2019-09-13 20:05:02 +08:00
|
|
|
|
|
|
|
protected $keyType = 'string';
|
2018-01-01 01:09:56 +08:00
|
|
|
public $incrementing = false;
|
2017-12-10 11:21:49 +08:00
|
|
|
|
2018-03-21 08:40:19 +08:00
|
|
|
protected $fillable = [
|
2017-12-10 11:21:49 +08:00
|
|
|
'name',
|
|
|
|
'key',
|
|
|
|
'value',
|
|
|
|
'group',
|
|
|
|
'type',
|
|
|
|
'options',
|
|
|
|
'description',
|
|
|
|
];
|
|
|
|
|
2018-01-04 12:04:51 +08:00
|
|
|
public static $rules = [
|
|
|
|
'name' => 'required',
|
|
|
|
'key' => 'required',
|
|
|
|
'group' => 'required',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return string
|
2018-01-04 12:04:51 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public static function formatKey($key): string
|
2018-01-01 01:19:18 +08:00
|
|
|
{
|
|
|
|
return str_replace('.', '_', strtolower($key));
|
|
|
|
}
|
|
|
|
|
2018-01-04 12:04:51 +08:00
|
|
|
/**
|
2018-03-19 09:37:35 +08:00
|
|
|
* Force formatting the key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-01-04 12:04:51 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function id(): Attribute
|
2018-01-01 01:09:56 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
get: fn ($id, $attrs) => self::formatKey(strtolower($id))
|
|
|
|
);
|
2018-01-01 01:09:56 +08:00
|
|
|
}
|
2018-01-07 05:41:23 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-19 09:37:35 +08:00
|
|
|
* Set the key to lowercase
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-01-07 05:41:23 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function key(): Attribute
|
2018-01-07 05:41:23 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($key) => strtolower($key)
|
|
|
|
);
|
2018-03-19 09:37:35 +08:00
|
|
|
}
|
2017-12-10 11:21:49 +08:00
|
|
|
}
|