io-digital / repo
Easily generate the Repository Pattern in Laravel
Installs: 2 170
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ~5.6|~7.0
- illuminate/support: ~5.1
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- dev-master / 1.0.x-dev
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-feature/package-autodiscovery
- dev-feature/controller-creation
- dev-feature/update-repo-functions
- dev-feature/where-in
- dev-feature/usage-update
- dev-feature/progress-bar
This package is not auto-updated.
Last update: 2021-05-09 14:48:59 UTC
README
This package creates scaffolding to implement the Repository pattern.
Install
Repo supports package auto discovery for Laravel
composer require io-digital/repo
Add the ServiceProvider to your config/app.php providers array:
IoDigital\Repo\RepoServiceProvider::class,
Then run the following artisan command:
$ php artisan vendor:publish --provider="IoDigital\Repo\RepoServiceProvider"
This will create the following folder structure in your app/
folder:
- Models
- Concrete
- Eloquent
- _AbstractEloquentRepository.php
- Contracts
- Repositories
- _RepositoryInterface.php
- Objects
- Concrete
Note that _AbstractEloquentRepository.php and _RepositoryInterface.php are named as such to avoid existing files being overwritten. In the case of a new installation, these files can simply be renamed to AbstractEloquentRepository.php and RepositoryInterface.php, respectively. If these files already exist, on the other hand, please take care to manually merge the newly published files with the existing ones.
Usage
After installing the package the artisan command repo:create
should be available.
To create the repository structure for your object run the command:
$ php artisan repo:create Post
This will create the following files:
- Models/Objects/Post.php
- Models/Contracts/Repositories/PostRepository.php
- Models/Concrete/Eloquent/EloquentPostRepository.php
It will also add the bindings in your AppServiceProvider.php
file:
$this->app->bind( 'App\Models\Contracts\Repositories\PostRepository', // Repository (Interface) 'App\Models\Concrete\Eloquent\EloquentPostRepository' // Eloquent (Class) );
Then in your Controller it's simply:
... use App\Models\Contracts\Repositories\PostRepository; ... protected $model; public function __construct(PostRepository $repo) { $this->model = $repo; }
Options
-m
or--m
-c
or--c
Use the -m
option to create a migration file for your object:
$ php artisan repo:create Post -m
Use the -c
option to create a resource controller for your object:
$ php artisan repo:create Post -c
All together now:
$ php artisan repo:create Post -m -c
The repository interface provides the following methods:
public function make($with = [], $orderBy = []); public function all($with = [], $orderBy = [], $columns = ['*']); public function find($id, $relations = []); public function findBy($attribute, $value, $columns = ['*']); public function findAllBy($attribute, $value, $columns = ['*']); public function findWhere($where, $columns = ['*'], $or = false); public function findWhereIn($field, array $values, $columns = ['*']); public function paginate($perPage = 25, $columns = ['*']); public function simplePaginate($limit = null, $columns = ['*']); public function create($attributes = []); public function edit($id, $attributes = []); public function delete($id);
The implementations can found in Models/Concrete/AbstractEloquentRepository.php
Example usage for the find functions:
//returns the model with the relationship as a paginated collection $data = $this->model->make(['relation'])->paginate(); //returns with ->first() $data = $this->model->findBy('title', $title); //returns with ->get() $data = $this->model->findAllBy('category', $category); //returns with ->get() $data = $this->model->findWhere([ 'category' => $category, ['year', '>' , $year], ['name', 'like', "%$name%"], ['surname', 'like', '%nes'] ]); $data = $this->model->findWhereIn('id', [1, 2, 3]) ->get(['name', 'email']);
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
TODO
Clean up code
License
The MIT License (MIT). Please see License File for more information.