Created
November 21, 2021 07:25
-
-
Save einnar82/50aac7594942199e684d3e5883c50cc8 to your computer and use it in GitHub Desktop.
Sample Repository Pattern for Laravel
This file contains 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\Pagination\Paginator; | |
abstract class BaseRepository | |
{ | |
protected Model $model; | |
public function __construct(Model $model) | |
{ | |
$this->model = $model; | |
} | |
/** | |
* @param array $attributes | |
* @return Model | |
*/ | |
public function create(array $attributes): ?Model | |
{ | |
return $this->model->create($attributes); | |
} | |
/** | |
* @param array $attributes | |
* @param Model $model | |
* @return Model | |
*/ | |
public function update(array $attributes, Model $model): ?Model | |
{ | |
return tap($model)->update($attributes); | |
} | |
/** | |
* @return Paginator | |
*/ | |
public function all(): ?Paginator | |
{ | |
return $this->model->orderBy('id', 'desc')->simplePaginate(); | |
} | |
/** | |
* @param Model $model | |
* @return Model | |
*/ | |
public function find(Model $model): ?Model | |
{ | |
return $model; | |
} | |
/** | |
* @param Model $model | |
* @return bool | |
*/ | |
public function deleteOne(Model $model): ?bool | |
{ | |
return $model->delete(); | |
} | |
} |
This file contains 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\Contracts; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Pagination\Paginator; | |
interface DatabaseRepositoryContract | |
{ | |
/** | |
* @param array $attributes | |
* @return Model | |
*/ | |
public function create(array $attributes): ?Model; | |
/** | |
* @param array $attributes | |
* @param Model $model | |
* @return Model | |
*/ | |
public function update(array $attributes, Model $model): ?Model; | |
/** | |
* @return Paginator | |
*/ | |
public function all(): ?Paginator; | |
/** | |
* @param Model $model | |
* @return Model | |
*/ | |
public function find(Model $model): ?Model; | |
/** | |
* @param Model $model | |
* @return bool | |
*/ | |
public function deleteOne(Model $model): ?bool; | |
} |
This file contains 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\Providers; | |
use App\Modules\Transfer\Contracts\TransferRepositoryContract; | |
use App\Modules\Transfer\Models\Transfer; | |
use App\Modules\Transfer\Repositories\TransferRepository; | |
use Illuminate\Support\ServiceProvider; | |
class RepositoryServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->registerRepositories(); | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
public function registerRepositories() | |
{ | |
$this->app->instance(TransferRepositoryContract::class, new TransferRepository(new Transfer())); | |
} | |
} |
This file contains 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\Modules\Transfer\Repositories; | |
use App\Modules\Transfer\Contracts\TransferRepositoryContract; | |
use App\Modules\Transfer\Models\Transfer; | |
use App\Repositories\BaseRepository; | |
class TransferRepository extends BaseRepository implements TransferRepositoryContract | |
{ | |
public function __construct(Transfer $model) | |
{ | |
parent::__construct($model); | |
} | |
} |
This file contains 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\Modules\Transfer\Contracts; | |
use App\Contracts\DatabaseRepositoryContract; | |
interface TransferRepositoryContract extends DatabaseRepositoryContract | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment