manchenkoff / laravel-repositories
Repository pattern implementation for your Laravel application
Requires
- php: ^8.2
- illuminate/console: ^11.0
- illuminate/contracts: ^11.0
- illuminate/database: ^11.0
- illuminate/support: ^11.0
Requires (Dev)
- driftingly/rector-laravel: ^0.26.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1
- nunomaduro/larastan: ^2.9
- orchestra/testbench: ^9.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10
- rector/rector: ^0.19
README
Package provides a basic implementation of Repository pattern with artisan
command to generate classes.
Features:
Repository
class with basic methods likeall
,find
,create
,update
,delete
- Generic type comments to pass
PHPStan
checks - Artisan
make:repository
command to generate repository class with model and interface
Installation
To install this package, you need to install Composer first, and then run:
composer require manchenkoff/laravel-repositories
or add this line to composer.json
:
"manchenkoff/laravel-repositories": "*"
and run composer update
in the terminal.
Package should automatically register its service provider in your application, but you can do it manually in config/app.php
:
'providers' => ServiceProvider::defaultProviders() ->merge([ // Package Service Providers \Manchenkoff\Laravel\Repositories\ServiceProvider::class, // Application Service Providers // ... ]) ->toArray(),
Usage
First of all, you need to create a model class for your repository. You can do it manually or use artisan
command:
php artisan make:model Post
Then you can create a repository class for your model:
# repository name - PostRepository # model name - Post php artisan make:repository PostRepository Post
This command will create a repository class in app/Repositories
directory and PostRepositoryInterface
contract class in app/Contracts/Repositories
.
Now you can use existing methods in your services or extend with custom functionality:
<?php namespace App\Services; use Illuminate\Database\Eloquent\Collection; use App\Contracts\Repositories\PostRepositoryInterface; use App\Contracts\Services\PostServiceInterface; final class PostService implements PostServiceInterface { private readonly PostRepositoryInterface $repository; public function __construct(PostRepositoryInterface $repository) { $this->repository = $repository; } public function getAllPosts(): Collection { return $this->repository->all(); } }
Implementation
All repository methods use protected query()
method to get Eloquent\Builder
instance. You can override this method in your repository class to add custom logic, e.g. when you always need some relations to be loaded or custom sorting applied.
protected function query(): Builder { return parent::query()->with('comments')->orderBy('created_at', 'desc'); }
Here is a list of available methods with a quick description:
Also you might want to adjust class template for your needs. You can do it by publishing package views:
php artisan vendor:publish --provider="Manchenkoff\Laravel\Repositories\ServiceProvider"
Development
This package is completely open-source, so any contributions are welcome!
Clone this repository to your local machine, install dependencies and run tests:
git clone https://github.com/manchenkoff/laravel-repositories cd laravel-repositories composer install composer test
There are some useful composer
scripts: