Last active
March 5, 2022 07:53
-
-
Save jalallinux/190ced657a8c8416a6b8dabc1ecb3432 to your computer and use it in GitHub Desktop.
Laravel: Base Repository Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Repositories\Eloquent; | |
use App\Repositories\BaseRepository; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\Model; | |
abstract class BaseEloquentRepository implements BaseRepository | |
{ | |
/** | |
* The model connection to Database | |
* | |
* @var Model | |
*/ | |
protected Model $model; | |
/** | |
* Get Model query builder instance | |
* | |
* @return Builder | |
*/ | |
public function query(): Builder | |
{ | |
return $this->model->query(); | |
} | |
/** | |
* Pass all unknown function through base model | |
* | |
* @param $method | |
* @param $args | |
* @return mixed | |
*/ | |
public function __call($method, $args) | |
{ | |
return call_user_func_array( | |
[$this->getModel(), $method], | |
$args | |
); | |
} | |
/** | |
* Set database model | |
* | |
* @param Model $model | |
* @return $this | |
*/ | |
public function setModel(Model $model): self | |
{ | |
$this->model = $model; | |
return $this; | |
} | |
/** | |
* Get database model | |
* | |
* @return Model | |
*/ | |
public function getModel(): Model | |
{ | |
return $this->model; | |
} | |
/** | |
* Get all Rows | |
* | |
* @param array $columns | |
* @return Collection | |
*/ | |
public function get(array $columns = ['*']): Collection | |
{ | |
return $this->query()->get($columns); | |
} | |
/** | |
* Wrap model findOrFail method | |
* | |
* @param $value | |
* @param string|null $column | |
* @return ?Model | |
*/ | |
public function findOrFail($value, string $column = null): ?Model | |
{ | |
return $this->query()->where($column ?? $this->model->getKeyName(), $value)->firstOrFail(); | |
} | |
/** | |
* Wrap model find method | |
* | |
* @param $value | |
* @param string|null $column | |
* @return ?Model | |
*/ | |
public function find($value, string $column = null): ?Model | |
{ | |
return $this->query()->where($column ?? $this->model->getKeyName(), $value)->first(); | |
} | |
/** | |
* Create a new item | |
* | |
* @param array $data | |
* @return mixed | |
*/ | |
public function create(array $data) | |
{ | |
return $this->query()->create($data); | |
} | |
/** | |
* Update an existing model instance | |
* | |
* @param Model $model | |
* @param $data | |
* @return bool | |
*/ | |
public function update(Model $model, $data): bool | |
{ | |
return $model->update($data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Repositories; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | |
use Illuminate\Support\Collection as SupportCollection; | |
interface BaseRepository | |
{ | |
/** | |
* Set database model | |
* | |
* @param Model $model | |
* @return $this | |
*/ | |
public function setModel(Model $model): self; | |
/** | |
* Get database model | |
* | |
* @return Model | |
*/ | |
public function getModel(): Model; | |
/** | |
* Get all Rows | |
* | |
* @param array $columns | |
* @return EloquentCollection|SupportCollection | |
*/ | |
public function get(array $columns = ['*']); | |
/** | |
* Wrap model findOrFail method | |
* | |
* @param $value | |
* @param string $column | |
* @return ?Model | |
*/ | |
public function findOrFail($value, string $column = 'id'): ?Model; | |
/** | |
* Wrap model find method | |
* | |
* @param $value | |
* @param string $column | |
* @return ?Model | |
*/ | |
public function find($value, string $column = 'id'): ?Model; | |
/** | |
* Create a new item | |
* | |
* @param array $data | |
* @return mixed | |
*/ | |
public function create(array $data); | |
/** | |
* Update an existing model instance | |
* | |
* @param Model $model | |
* @param $data | |
* @return bool | |
*/ | |
public function update(Model $model, $data): bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment