ArticleRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Article;
  4. use Prettus\Repository\Eloquent\BaseRepository;
  5. /**
  6. * Class ArticleRepositoryEloquent.
  7. *
  8. * @package namespace App\Repositories;
  9. */
  10. class ArticleRepository extends BaseRepository
  11. {
  12. /**
  13. * Specify Model class name
  14. *
  15. * @return string
  16. */
  17. public function model()
  18. {
  19. return Article::class;
  20. }
  21. /**
  22. * @param $where array
  23. * @return null|\Illuminate\Database\Eloquent\Model
  24. */
  25. public function getArticles($where, $page)
  26. {
  27. /*$res = $this->model->when(get_subsite_id()>0, function ($query) {
  28. $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
  29. })->where($where)->orderByRaw('list_order asc,id desc');*/
  30. $res = $this->model->whereHas('subsites', function ($query) {
  31. $query->where('subsite_id', get_subsite_id());
  32. })->where($where)->orderByRaw('list_order desc,created_at desc');
  33. if ($page) {
  34. return $res->paginate($page);
  35. } else {
  36. return $res->get();
  37. }
  38. }
  39. /*
  40. *根据分类查询
  41. */
  42. public function getArticle($where,$page)
  43. {
  44. return $this->model->whereHas('show_category',function ($query) use ($where){
  45. $query->where($where);
  46. })->paginate($page);
  47. }
  48. /*
  49. *根据分类查询
  50. */
  51. public function getArticlesByType($type_id,$limit)
  52. {
  53. $where = array('is_display'=>'1','type_id'=>$type_id);
  54. return $this->model->selectRaw('id,small_img')->whereHas('subsites', function ($query) {
  55. $query->where('subsite_id', get_subsite_id());
  56. })->where($where)->where('small_img','<>','')->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();
  57. }
  58. /**
  59. * @param $where array
  60. * @return null|\Illuminate\Database\Eloquent\Model
  61. */
  62. public function getAllArticles($where, $orWhere, $page)
  63. {
  64. /*$res = $this->model->when(get_subsite_id()>0, function ($query) {
  65. $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
  66. })->where(function ($query) use ($where, $orWhere) {
  67. $query->where($where)->orwhere($orWhere);
  68. })->orderByRaw('list_order asc,id desc');*/
  69. $res = $this->model->whereHas('subsites', function ($query) {
  70. $query->where('subsite_id', get_subsite_id());
  71. })->where(function ($query) use ($where, $orWhere) {
  72. $query->where($where)->orwhere($orWhere);
  73. })->orderByRaw('list_order desc,created_at desc');
  74. if ($page) {
  75. return $res->paginate($page);
  76. } else {
  77. return $res->get();
  78. }
  79. }
  80. public function getPropertyArticles($property_id, $limit)
  81. {
  82. $where = array('is_display'=>'1','property_id'=>$property_id);
  83. /*return $this->model->when(get_subsite_id()>0, function ($query) {
  84. $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
  85. })->where($where)->orderByRaw('list_order asc,id desc')->limit($limit)->get();*/
  86. return $this->model->whereHas('subsites', function ($query) {
  87. $query->where('subsite_id', get_subsite_id());
  88. })->where($where)->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();
  89. }
  90. public function firstWhere($where)
  91. {
  92. return $this->model->where($where)->first();
  93. }
  94. public function incrementData($where, $num, $filed)
  95. {
  96. return $this->model->where($where)->increment($filed, $num);
  97. }
  98. public function filterArticles($where = array(), $whereIn = array(), $order = array())
  99. {
  100. /*$rst = $this->model->where($where);*/
  101. /*$rst = $this->model->when(get_subsite_id()>0, function ($query) {
  102. $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
  103. })->where($where);*/
  104. $rst = $this->model->whereHas('subsites', function ($query) {
  105. $query->where('subsite_id', get_subsite_id());
  106. })->where($where);
  107. if ($whereIn) {
  108. foreach ($whereIn as $k => $v) {
  109. $rst->whereIn($k, $v);
  110. }
  111. }
  112. if ($order) {
  113. if (is_array($order)) {
  114. foreach ($order as $k => $v) {
  115. $rst->orderBy($k, $v);
  116. }
  117. } else {
  118. $rst->orderbyRaw($order);
  119. }
  120. }
  121. return $rst->get();
  122. }
  123. }