Config.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 配置模型
  6. *
  7. */
  8. class Config extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'config';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. /**
  16. * 添加数据
  17. *
  18. */
  19. public function add($data)
  20. {
  21. // 写入数据表
  22. $id = $this->query()->insertGetId($data);
  23. // 更新缓存
  24. if( $id ) $this->getConfigList(true);
  25. // 返回结果
  26. return $id;
  27. }
  28. /**
  29. * 添加数据
  30. *
  31. */
  32. public function edit($id,$data)
  33. {
  34. // 写入数据表
  35. $result = $this->query()->where(['id'=>$id])->update($data);
  36. // 更新缓存
  37. if( $result ) $this->getConfigList(true);
  38. // 返回结果
  39. return $result;
  40. }
  41. /**
  42. * 获取配置列表数据
  43. *
  44. * @return Array 配置列表
  45. */
  46. public function getConfigList($focer =false){
  47. // 缓存读取
  48. $config = $focer ? [] : cache('config::list');
  49. // 获取数据转数组
  50. if( !$config ) {
  51. // 数据库读取
  52. $config = $this->query()->where([['status','=',1]])->pluck('value','name')->toArray();
  53. // 存储
  54. cache(['config::list'=>$config]);
  55. }
  56. // 返回配置数据
  57. return $config;
  58. }
  59. }