ArticleCategoryRepository.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\ArticleCategory;
  4. use Prettus\Repository\Eloquent\BaseRepository;
  5. /**
  6. * Class ArticleCategoryRepositoryEloquent.
  7. *
  8. * @package namespace App\Repositories;
  9. */
  10. class ArticleCategoryRepository extends BaseRepository
  11. {
  12. /**
  13. * Specify Model class name
  14. *
  15. * @return string
  16. */
  17. public function model()
  18. {
  19. return ArticleCategory::class;
  20. }
  21. /**
  22. * @param $where array $limit int
  23. * @return null|\Illuminate\Database\Eloquent\Model
  24. */
  25. public function getChildCategorys($where, $limit)
  26. {
  27. return $this->model->where($where)->orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->limit($limit)->get();
  28. }
  29. public function getInfo($id)
  30. {
  31. $info = $this->model->where('id', $id)->first();
  32. return $info;
  33. }
  34. public function getBread($id)
  35. {
  36. $info = $this->model->where('id', $id)->first();
  37. $res = [['id' => $info['id'], 'name' => $info['category_name']]];
  38. if ($info['parent_id'] != 0) {
  39. $this->_getBread($info, $res);
  40. }
  41. return $res;
  42. }
  43. private function _getBread($info, &$res)
  44. {
  45. $cate = $this->model->where('id', $info['parent_id'])->first();
  46. array_unshift($res, ['id' => $cate['id'], 'name' => $cate['category_name']]);
  47. if ($cate['parent_id'] != 0) {
  48. $this->_getBread($cate, $res);
  49. }
  50. }
  51. }