12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 配置模型
- *
- */
- class Config extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'config';
- // 是否主动维护时间戳
- public $timestamps = false;
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 更新缓存
- if( $id ) $this->getConfigList(true);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 更新缓存
- if( $result ) $this->getConfigList(true);
- // 返回结果
- return $result;
- }
- /**
- * 获取配置列表数据
- *
- * @return Array 配置列表
- */
- public function getConfigList($focer =false){
- // 缓存读取
- $config = $focer ? [] : cache('config::list');
- // 获取数据转数组
- if( !$config ) {
- // 数据库读取
- $config = $this->query()->where([['status','=',1]])->pluck('value','name')->toArray();
- // 存储
- cache(['config::list'=>$config]);
- }
- // 返回配置数据
- return $config;
- }
- }
|