DictRepositoryEloquent.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Repositories\Eloquent\Base;
  3. use App\Contracts\Repositories\Base\DictRepository;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Base\Dict;
  6. use Prettus\Repository\Criteria\RequestCriteria;
  7. use Prettus\Repository\Eloquent\BaseRepository;
  8. class DictRepositoryEloquent extends BaseRepository implements DictRepository
  9. {
  10. protected $fieldSearchable = [
  11. // 'name' => 'like', Default Condition "="
  12. ];
  13. /**
  14. * Specify Model class name.
  15. *
  16. * @return string
  17. */
  18. public function model()
  19. {
  20. return Dict::class;
  21. }
  22. /**
  23. * Boot up the repository, pushing criteria.
  24. *
  25. * @throws \Prettus\Repository\Exceptions\RepositoryException
  26. */
  27. public function boot()
  28. {
  29. $this->pushCriteria(app(RequestCriteria::class));
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function searchDictsByPage()
  35. {
  36. return $this->paginate(request('per_page', 15));
  37. }
  38. /**
  39. * @param $id
  40. *
  41. * @return mixed
  42. */
  43. public function searchDictBy($id)
  44. {
  45. return $this->find($id);
  46. }
  47. /**
  48. * 通过key获取配置项
  49. * @param $keys
  50. * @return mixed
  51. */
  52. public function byKeysConfigs($keys)
  53. {
  54. return $this->whereIn('code', $keys)->with(['detail' => function ($query) {
  55. return $query->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['id', 'name', 'value', 'dict_id']);
  56. }])->select(['id', 'name', 'code'])->get();
  57. }
  58. /**
  59. * 获取单个key
  60. * @param $key
  61. * @return mixed
  62. */
  63. public function byKeysConfig($key)
  64. {
  65. return $this->where('code', $key)->with(['detail' => function ($query) {
  66. return $query->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['id', 'name', 'value', 'dict_id']);
  67. }])->select(['id', 'name', 'code'])->first();
  68. }
  69. }