Repositories is a simple package to simplify the creation of scaffolding code when using the Repository Design Pattern. It adds an artisan command which creates the repository and the related contract.
You can install the package via composer:
composer require nalingia/laravel-repositories
Then you have to add the related Service Provider to the providers
configuration array in config/app.php
:
'providers' => [
...
Nalingia\Repositories\RepositoriesServiceProvider::class,
...
]
The package provides an additional artisan command called make:repository
which accepts as only parameter the model's class name.
For instance, if you need to create a repository of users you can type:
php artisan make:repository User
The command will create both the repository and the related contract in app/Repositories
and app/Repositories/Contracts
folders, respectively.
Default folders and namespaces can be changed
The generated repositories will inherit from Nalingia\Repositories\AbstractEloquentRepository
which provides a bunch of useful and general purpose methods:
- Get all models:
public function all(array $with = []);
- Get model having the given id:
public function findById($id, array $with = [], $fail = true);
- Get the first model having
$key
attribute equals to$value
:
public function getFirstBy($key, $value, array $with = [], $comparator = '=', $fail = false);
- Get all models having
$key
attribute equals to$value
:
public function getManyBy($key, $value, array $with = [], $comparator = '=');
- Get first model matching
$where
clauses:
public function getFirstWhere(array $where, $with = [], $fail = false);
- Get all models matching
$where
clauses:
public function getAllWhere($where, $with = [], $columns = ['*']);
- Get paginated models:
public function getByPage($page = 1, $limit = 10, array $with = []);
- Create a new model:
public function create(array $data);
- Update a model:
public function update($model, array $data);
- Delete a model:
public function delete($model);
- Truncate the table related to the model:
public function truncate();
- Get all models having
column_name
in$needles
:
public function getAllWhereColumnNameIn(array $needles, array $with = []);
You can contribute to the common methods by proposing a pull request.
If you need to change the default command settings, you can publish the repositories.php
configuration file using the command:
php artisan vendor:publish --provider="Nalingia\Repositories\RepositoriesServiceProvider"
Please, see CHANGELOG for more information about what has changed recently.
Please see CONTRIBUTING for details.
You can run the tests with:
composer test
or
vendor/bin/phpunit
The MIT License (MIT). Please see License File for more information.