Skip to content

nalingia/laravel-repositories

Repository files navigation

A Laravel package for the Repository design pattern

Latest Version on Packagist Build Status Software License Total Downloads

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.

Installation

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,
  ...
]

Usage

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

Available API

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.

Settings

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"

Change log

Please, see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Testing

You can run the tests with:

composer test

or

vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

About

A Laravel package for the Repository Design Pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages