SystemRepositoryEloquent.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Repositories;
  3. use Prettus\Repository\Eloquent\BaseRepository;
  4. use Prettus\Repository\Criteria\RequestCriteria;
  5. use App\Repositories\SystemRepository;
  6. use App\Models\System;
  7. use Illuminate\Container\Container as Application;
  8. /**
  9. * Class SystemRepositoryEloquent
  10. * @package namespace App\Repositories;
  11. */
  12. class SystemRepositoryEloquent extends BaseRepository implements SystemRepository
  13. {
  14. private $config;
  15. public function __construct(Application $app)
  16. {
  17. parent::__construct($app);
  18. $this->config = config('blog.system_key');
  19. }
  20. /**
  21. * Specify Model class name
  22. *
  23. * @return string
  24. */
  25. public function model()
  26. {
  27. return System::class;
  28. }
  29. /**
  30. * Boot up the repository, pushing criteria
  31. */
  32. public function boot()
  33. {
  34. $this->pushCriteria(app(RequestCriteria::class));
  35. }
  36. /**
  37. * 保存博客设置
  38. *
  39. * @param array $data
  40. * @return bool
  41. */
  42. public function store(array $data)
  43. {
  44. if (!$data) {
  45. return false;
  46. }
  47. unset($data['_token']);
  48. foreach ($data as $key => $value) {
  49. if (in_array($key, $this->config)) {
  50. $this->updateOrCreate(['key' => $key], ['value' => $value]);
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * 获取选项列表
  57. *
  58. * @return array
  59. */
  60. public function optionList()
  61. {
  62. $all = $this->all(['key', 'value']);
  63. $system = $this->initSystemKey();
  64. foreach ($all as $a) {
  65. $system[$a['key']] = $a['value'];
  66. }
  67. return $system;
  68. }
  69. /**
  70. * @return array
  71. */
  72. private function initSystemKey()
  73. {
  74. $init = [];
  75. $config = array_flip($this->config);
  76. foreach ($config as $key => $value) {
  77. $init[$key] = '';
  78. }
  79. return $init;
  80. }
  81. }