Setting.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Setting extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'base_settings';
  9. //todo 对数据库做简单的封装 get和set(管理端会用到) 加缓存
  10. /**
  11. * @param String $tags
  12. * @param String $key
  13. * @param String $default
  14. * @return array|String
  15. */
  16. public static function get(string $tags, string $key = '', bool $is_string = false)
  17. {
  18. if($tags){
  19. $map[] = ['tags','=',$tags];
  20. }
  21. if($key){
  22. $map[] = ['key','=',$key];
  23. }
  24. $map[] = ['is_delete','=',0];
  25. if($is_string){
  26. $list = self::where($map)->select('title', 'key', 'value','tags')->get();
  27. $settings = [];
  28. foreach($list as $key=>&$val){
  29. $settings[$val['tags']][] = $val;
  30. }
  31. }else{
  32. $list = self::where($map)->select('title', 'key', 'value','tags')->get()->toArray();
  33. $settings = [];
  34. foreach($list as $key=>&$val){
  35. $val['value'] = json_decode($val['value']);
  36. $settings[$val['tags']][] = $val;
  37. }
  38. }
  39. return $settings;
  40. }
  41. /**
  42. * @param String $tags
  43. * @param String $key
  44. * @param String $value
  45. * @return int
  46. */
  47. public static function set(string $tags, string $key, string $value)
  48. {
  49. return self::where([['tags', $tags], ['key', $key]])->update(['value' => $value]);
  50. }
  51. }