2017 © Pedro PelĆ”ez
 

library repositories

Flexable Laravel ~5.0 Repository Library.

image

sebastian-berc/repositories

Flexable Laravel ~5.0 Repository Library.

  • Thursday, July 21, 2016
  • by sebastian-berc
  • Repository
  • 9 Watchers
  • 18 Stars
  • 19,255 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 8 Forks
  • 3 Open issues
  • 6 Versions
  • 14 % Grown

The README.md

Repositories

Latest Version on Packagist Software License Build Status Coverage Status SensioLabs Insight Quality Score Total Downloads, (*1)

Install

Via Composer, (*2)

``` bash $ composer require sebastian-berc/repositories, (*3)


## Usage Make your own repository with extends the abstract `\SebastianBerc\Repositories\Repository` class and implement `takeModel` method: ``` php class MyRepository extends \SebastianBerc\Repositories\Repository { /** * Return fully qualified model class name. * * @return string */ public function takeModel() { return MyModel::class } }

Usage with Dependency Incjection

Wherever Laravel provides dependency injection you can attach your repository like this:, (*4)

``` php class UsersController extends Controller { /** * Contains users repository. * * @var UsersRepository */ protected $repository;, (*5)

/**
 * Creates a new instance of users controller.
 *
 * @param UsersRepository $repository
 */
public function __construct(UsersRepository $repository)
{
    $this->repository = $repository;
}

}, (*6)


#### Usage without Dependency Injection If you need a repository without dependency injection you can use the static method or just construct new instance like this: ``` php /** * Creates a new users. */ public function store(Request $request) { $repository = UsersRepository::instance(); $repository = new UsersRepository(); return $repository->create($request->all()); }

Methods

This gives you access to methods such as..., (*7)

creating a new query to get all results from repository:, (*8)

``` php // Definition: $repository->all(array $columns = ['*']); // Example: $users = $repository->all(['name', 'value']);, (*9)


[Need fix] creating a new basic where query clause on model and returns results: ``` php // Definition: $repository->where($column, $operator = '=', $value = null, $boolean = 'and', array $columns = ['*']); // Example: $repository->where('id', '<>', \Auth::user()->getKey(), 'and', ['activated', 'banned']);

creating a new query with pagination:, (*10)

``` php // Definition: $repository->paginate($perPage = 15, array $columns = ['*']); // Example: $repository->paginate(50, ['name', 'value']);, (*11)


saving a new model and return the instance: ``` php // Definition: $repository->create(array $attributes = []); // Example: $repository->create(['activated' => true, 'banned' => false]);

saving or updates the model in the database., (*12)

``` php // Definition: $repository->update($identifier, array $attributes = []); // Example: $repository->update(1, ['activated' => true, 'banned' => false]);, (*13)

Also you can pass a model:

``` php
// Definition:
$repository->update($dirtyModel);
// Example:
$model = $repository->find(1);
$model->activated = true;
$repository->update($model);

even with additional attributes:, (*14)

``` php // Definition: $repository->update($dirtyModel, ['activated' => true]); // Example: $model = $repository->find(1); $model->activated = true; $repository->update($model, ['banned' => false]);, (*15)


delete the model from the database: ``` php // Definition: $repository->delete($identifier); // Example: $repository->delete(1);

find a model (only first result) by its primary key:, (*16)

``` php // Definition: $repository->find($identifier, array $columns = ['*']); // Example: $repository->find(1, ['name', 'value']);, (*17)


find a model (only first result) by its specified column and value: ``` php // Definition: $repository->findBy($column, $value, array $columns = ['*']); // Example: $repository->findBy('activated', true, ['id']);

find a model (only first result) by its specified columns and values presented as array:, (*18)

``` php // Definition: $repository->findWhere(array $wheres, array $columns = ['*']); // Example: $repository->findWhere(['activated' => true, 'banned' => false], ['id']);, (*19)


return total count of whole collection based on current query: ``` php // Definition and example: $repository->count();

fetch collection ordered and filtrated by specified columns for specified page and this method will return instance of LengthAwarePaginator:, (*20)

``` php // Definition: $repository->fetch($page = 1, $perPage = 15, array $columns = [''], array $filter = [], array $sort = []); // Example: $repository->fetch(1, 15, [''], ['activated' => true, 'banned' => 'false'], ['id' => 'ASC']);, (*21)


fetch simple collection without `LengthAwarePaginator`, but ordered and filtrated by specified columns for specified page: ``` php // Definition: $repository->simpleFetch($page = 1, $perPage = 15, array $columns = ['*'], array $filter = [], array $sort = []); // Example: $repository->simpleFetch(1, 15, ['*'], ['activated' => true, 'banned' => 'false'], ['id' => 'ASC']);

Eager loads

If your model has a relationship and you want to load it, you can do this with query results by calling the with method, for example:, (*22)

``` php // Definition: $repository->with($relations); // Examples: $repository->with('roles')->all(); $repository->with('roles', 'permissions')->all(); $repository->with(['roles', 'permissions])->all();, (*23)


#### Criteria Sometimes you need to prepare repeatable query that displays, for example, only active and not banned users or the latest news from the week before - to do this you can use a criteria: ``` php class ActivatedAndNotBanned extends \SebastianBerc\Repositories\Criteria { public function execute(Builder $query) { return $query->where(['activated' => true])->andWhere(['banned' => false]); } }

Now you can use your criteria with repository:, (*24)

``` php $repository->criteria(new ActivatedAndNotBanned())->all();, (*25)


#### Transformers The data from your repositories can be sent to different places, for example, it may be your site or the same data with the exclusion of several columns can be shared with Web API, you can achieve through class `Transfomer`: ``` php class UsersForListing extends \SebastianBerc\Repositories\Transformer { public function transform($item) { $item->fullname = $item->firstName . ' ' . $item->lastName; $item->password = '*****'; return $item; } }

When you have your own Transformer you can apply it to repository:, (*26)

``` php $repository->setTransformer(UsersForListing::class)->all();, (*27)



#### Cached repository results To hold query results in a cache just add the implementation of the interface `ShouldCache` to your repository: ``` php class MyRepository extends \SebastianBerc\Repositories\Repository implements \SebastianBerc\Repositories\Contracts\ShouldCache { /** * Return fully qualified model class name. * * @return string */ public function takeModel() { return MyModel::class; } }

And its magic because from now everything will be cached :)., (*28)

Change log

Please see CHANGELOG for more information what has changed recently., (*29)

Testing

bash $ composer test, (*30)

Contributing

Please see CONTRIBUTING for details., (*31)

Security

If you discover any security related issues, please email contact@sebastian-berc.pl instead of using the issue tracker., (*32)

Credits

License

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

The Versions