phpvms/app/Models/Setting.php

76 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string id
* @property string name
* @property string key
* @property string value
* @property string group
* @property string type
* @property string options
* @property int order
* @property string description
*/
class Setting extends Model
{
public $table = 'settings';
protected $keyType = 'string';
public $incrementing = false;
2018-03-21 08:40:19 +08:00
protected $fillable = [
'name',
'key',
'value',
'group',
'type',
'options',
'description',
];
public static $rules = [
'name' => 'required',
'key' => 'required',
'group' => 'required',
];
/**
* @param $key
2018-08-27 00:40:04 +08:00
*
* @return string
*/
public static function formatKey($key): string
2018-01-01 01:19:18 +08:00
{
return str_replace('.', '_', strtolower($key));
}
/**
* Force formatting the key
2018-08-27 00:40:04 +08:00
*
* @return Attribute
*/
public function id(): Attribute
{
return Attribute::make(
get: fn ($id, $attrs) => self::formatKey(strtolower($id))
);
}
/**
* Set the key to lowercase
2018-08-27 00:40:04 +08:00
*
* @return Attribute
*/
public function key(): Attribute
{
return Attribute::make(
set: fn ($key) => strtolower($key)
);
}
}