EloquentBaseRepository.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\common\repositories\Eloquent;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use app\common\repositories\BaseRepository;
  6. /**
  7. * Class EloquentCoreRepository
  8. *
  9. * @package app\common\repositories\Eloquent
  10. */
  11. abstract class EloquentBaseRepository implements BaseRepository
  12. {
  13. /**
  14. * @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model
  15. */
  16. protected $model;
  17. /**
  18. * @param Model $model
  19. */
  20. public function __construct($model)
  21. {
  22. $this->model = $model;
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function find($id)
  28. {
  29. if (method_exists($this->model, 'translations')) {
  30. return $this->model->with('translations')->find($id);
  31. }
  32. return $this->model->find($id);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function all()
  38. {
  39. if (method_exists($this->model, 'translations')) {
  40. return $this->model->with('translations')->orderBy('created_at', 'DESC')->get();
  41. }
  42. return $this->model->orderBy('created_at', 'DESC')->get();
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function paginate($perPage = 15)
  48. {
  49. if (method_exists($this->model, 'translations')) {
  50. return $this->model->with('translations')->orderBy('created_at', 'DESC')->paginate($perPage);
  51. }
  52. return $this->model->orderBy('created_at', 'DESC')->paginate($perPage);
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function create($data)
  58. {
  59. return $this->model->create($data);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function update($model, $data)
  65. {
  66. $model->update($data);
  67. return $model;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function destroy($model)
  73. {
  74. return $model->delete();
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function allTranslatedIn($lang)
  80. {
  81. return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
  82. $q->where('locale', "$lang");
  83. })->with('translations')->orderBy('created_at', 'DESC')->get();
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. public function findBySlug($slug)
  89. {
  90. if (method_exists($this->model, 'translations')) {
  91. return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
  92. $q->where('slug', $slug);
  93. })->with('translations')->first();
  94. }
  95. return $this->model->where('slug', $slug)->first();
  96. }
  97. /**
  98. * @inheritdoc
  99. */
  100. public function findByAttributes(array $attributes)
  101. {
  102. $query = $this->buildQueryByAttributes($attributes);
  103. return $query->first();
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
  109. {
  110. $query = $this->buildQueryByAttributes($attributes, $orderBy, $sortOrder);
  111. return $query->get();
  112. }
  113. /**
  114. * Build Query to catch resources by an array of attributes and params
  115. * @param array $attributes
  116. * @param null|string $orderBy
  117. * @param string $sortOrder
  118. * @return \Illuminate\Database\Eloquent\Builder
  119. */
  120. private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
  121. {
  122. $query = $this->model->query();
  123. if (method_exists($this->model, 'translations')) {
  124. $query = $query->with('translations');
  125. }
  126. foreach ($attributes as $field => $value) {
  127. $query = $query->where($field, $value);
  128. }
  129. if (null !== $orderBy) {
  130. $query->orderBy($orderBy, $sortOrder);
  131. }
  132. return $query;
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function findByMany(array $ids)
  138. {
  139. $query = $this->model->query();
  140. if (method_exists($this->model, 'translations')) {
  141. $query = $query->with('translations');
  142. }
  143. return $query->whereIn("id", $ids)->get();
  144. }
  145. /**
  146. * @inheritdoc
  147. */
  148. public function clearCache()
  149. {
  150. return true;
  151. }
  152. }