| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace app\common\repositories\Eloquent;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Builder;
- use app\common\repositories\BaseRepository;
- /**
- * Class EloquentCoreRepository
- *
- * @package app\common\repositories\Eloquent
- */
- abstract class EloquentBaseRepository implements BaseRepository
- {
- /**
- * @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model
- */
- protected $model;
- /**
- * @param Model $model
- */
- public function __construct($model)
- {
- $this->model = $model;
- }
- /**
- * @inheritdoc
- */
- public function find($id)
- {
- if (method_exists($this->model, 'translations')) {
- return $this->model->with('translations')->find($id);
- }
- return $this->model->find($id);
- }
- /**
- * @inheritdoc
- */
- public function all()
- {
- if (method_exists($this->model, 'translations')) {
- return $this->model->with('translations')->orderBy('created_at', 'DESC')->get();
- }
- return $this->model->orderBy('created_at', 'DESC')->get();
- }
- /**
- * @inheritdoc
- */
- public function paginate($perPage = 15)
- {
- if (method_exists($this->model, 'translations')) {
- return $this->model->with('translations')->orderBy('created_at', 'DESC')->paginate($perPage);
- }
- return $this->model->orderBy('created_at', 'DESC')->paginate($perPage);
- }
- /**
- * @inheritdoc
- */
- public function create($data)
- {
- return $this->model->create($data);
- }
- /**
- * @inheritdoc
- */
- public function update($model, $data)
- {
- $model->update($data);
- return $model;
- }
- /**
- * @inheritdoc
- */
- public function destroy($model)
- {
- return $model->delete();
- }
- /**
- * @inheritdoc
- */
- public function allTranslatedIn($lang)
- {
- return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
- $q->where('locale', "$lang");
- })->with('translations')->orderBy('created_at', 'DESC')->get();
- }
- /**
- * @inheritdoc
- */
- public function findBySlug($slug)
- {
- if (method_exists($this->model, 'translations')) {
- return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
- $q->where('slug', $slug);
- })->with('translations')->first();
- }
- return $this->model->where('slug', $slug)->first();
- }
- /**
- * @inheritdoc
- */
- public function findByAttributes(array $attributes)
- {
- $query = $this->buildQueryByAttributes($attributes);
- return $query->first();
- }
- /**
- * @inheritdoc
- */
- public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
- {
- $query = $this->buildQueryByAttributes($attributes, $orderBy, $sortOrder);
- return $query->get();
- }
- /**
- * Build Query to catch resources by an array of attributes and params
- * @param array $attributes
- * @param null|string $orderBy
- * @param string $sortOrder
- * @return \Illuminate\Database\Eloquent\Builder
- */
- private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
- {
- $query = $this->model->query();
- if (method_exists($this->model, 'translations')) {
- $query = $query->with('translations');
- }
- foreach ($attributes as $field => $value) {
- $query = $query->where($field, $value);
- }
- if (null !== $orderBy) {
- $query->orderBy($orderBy, $sortOrder);
- }
- return $query;
- }
- /**
- * @inheritdoc
- */
- public function findByMany(array $ids)
- {
- $query = $this->model->query();
- if (method_exists($this->model, 'translations')) {
- $query = $query->with('translations');
- }
- return $query->whereIn("id", $ids)->get();
- }
- /**
- * @inheritdoc
- */
- public function clearCache()
- {
- return true;
- }
- }
|