CategoryRepositoryEloquent.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Repositories;
  3. use Prettus\Repository\Eloquent\BaseRepository;
  4. use Prettus\Repository\Criteria\RequestCriteria;
  5. use App\Repositories\CategoryRepository;
  6. use App\Models\Category;
  7. use App\Validators\CategoryValidator;
  8. /**
  9. * Class CategoryRepositoryEloquent
  10. * @package namespace App\Repositories;
  11. */
  12. class CategoryRepositoryEloquent extends BaseRepository implements CategoryRepository
  13. {
  14. /**
  15. * Specify Model class name
  16. *
  17. * @return string
  18. */
  19. public function model()
  20. {
  21. return Category::class;
  22. }
  23. /**
  24. * @return mixed
  25. */
  26. public function getNestedList()
  27. {
  28. return $this->model->getNestedList('name', null, '&nbsp;&nbsp;&nbsp;&nbsp;');
  29. }
  30. /**
  31. * @param $input
  32. * @return bool
  33. */
  34. public function store($input)
  35. {
  36. if ($input['cate_id'] == 0) {
  37. return $this->model->create(['name' => $input['name']]) ? true : false;
  38. }
  39. $category = $this->model->find($input['cate_id']);
  40. if (!$category) {
  41. return false;
  42. }
  43. return $category->children()->create(['name' => $input['name']]) ? true : false;
  44. }
  45. /**
  46. * 分类修改
  47. *
  48. * @param array $attributes
  49. * @param $id
  50. * @return bool
  51. */
  52. public function update(array $attributes, $id)
  53. {
  54. $input['name'] = $attributes['name'];
  55. $parentId = $attributes['cate_id'];
  56. $category = $this->model->find($id);
  57. if (!$category) {
  58. return false;
  59. }
  60. if (!parent::update($input, $id)) {
  61. return false;
  62. }
  63. if ($parentId != 0 && $category->parent_id != $parentId) {
  64. $parentCategory = $this->model->find($parentId);
  65. if (!$parentCategory) {
  66. return false;
  67. }
  68. if (!$category->makeChildOf($parentCategory)) {
  69. return false;
  70. }
  71. } elseif ($category->parent_id != $parentId && $parentId == 0) {
  72. //顶级分类
  73. if (!$category->makeRoot()) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * @param $id
  81. * @param array $columns
  82. * @return mixed
  83. */
  84. public function find($id, $columns = ['*'])
  85. {
  86. $this->applyCriteria();
  87. $this->applyScope();
  88. $model = $this->model->find($id, $columns);
  89. $this->resetModel();
  90. return $this->parserResult($model);
  91. }
  92. public function baseFind($id, $columns = ['*'])
  93. {
  94. return parent::find($id, $columns);
  95. }
  96. /**
  97. * Boot up the repository, pushing criteria
  98. */
  99. public function boot()
  100. {
  101. $this->pushCriteria(app(RequestCriteria::class));
  102. }
  103. }