Create a Repository

Generators

Create your repositories easily through the generator.

Config

You must first configure the storage location of the repository files. By default is the "app" folder and the namespace "App". Please note that, values in the paths array are acutally used as both namespace and file paths. Relax though, both foreward and backward slashes are taken care of during generation.

Commands

To generate everything you need for your Model, run this command:

php artisan make:entity Post

This will create the Controller, the Validator, the Model, the Repository, the Presenter and the Transformer classes.
It will also create a new service provider that will be used to bind the Eloquent Repository with its corresponding Repository Interface.
To load it, just add this to your AppServiceProvider@register method:

$this->app->register(RepositoryServiceProvider::class);

You can also pass the options from the repository command, since this command is just a wrapper.

To generate a repository for your Post model, use the following command

php artisan make:repository Post

To generate a repository for your Post model with Blog namespace, use the following command

php artisan make:repository "Blog\Post"

Added fields that are fillable

php artisan make:repository "Blog\Post" --fillable="title,content"

To add validations rules directly with your command you need to pass the --rules option and create migrations as well:

Done , done that your repository will be cached , and the repository cache is cleared whenever an item is created, modified or deleted.

Cache Config

You can change the cache settings in the file config/repository.php and also directly on your repository.

config/repository.php

'cache'=>[//Enable or disable cache repositories'enabled'=>true,//Lifetime of cache'minutes'=>30,//Repository Cache, implementation Illuminate\Contracts\Cache\Repository'repository'=>'cache',//Sets clearing the cache'clean'=> [//Enable, disable clearing the cache on changes'enabled'=>true,'on'=> [//Enable, disable clearing the cache when you create an item'create'=>true,//Enable, disable clearing the cache when upgrading an item'update'=>true,//Enable, disable clearing the cache when you delete an item'delete'=>true, ] ],'params'=> [//Request parameter that will be used to bypass the cache repository'skipCache'=>'skipCache' ],'allowed'=>[//Allow caching only for some methods'only'=>null,//Allow caching for all available methods, except'except'=>null ],],

It is possible to override these settings directly in the repository.

usePrettus\Repository\Eloquent\BaseRepository;usePrettus\Repository\Contracts\CacheableInterface;usePrettus\Repository\Traits\CacheableRepository;classPostRepositoryextendsBaseRepositoryimplementsCacheableInterface {// Setting the lifetime of the cache to a repository specificallyprotected$cacheMinutes=90;protected$cacheOnly= ['all', ...];//orprotected$cacheExcept= ['find', ...];useCacheableRepository;...}

Using the presenter after from the Model

If you recorded a presenter and sometime used the skipPresenter() method or simply you do not want your result is not changed automatically by the presenter.
You can implement Presentable interface on your model so you will be able to present your model at any time. See below:

In your model, implement the interface Prettus\Repository\Contracts\Presentable and Prettus\Repository\Traits\PresentableTrait

$repository= app('App\PostRepository');$repository->setPresenter("Prettus\\Repository\\Presenter\\ModelFractalPresenter");//Getting the result transformed by the presenter directly in the search$post=$repository->find(1);print_r( $post ); //It produces an output as array...//Skip presenter and bringing the original result of the Model$post=$repository->skipPresenter()->find(1);print_r( $post ); //It produces an output as a Model objectprint_r( $post->presenter() ); //It produces an output as array

You can skip the presenter at every visit and use it on demand directly into the model, for it set the $skipPresenter attribute to true in your repository: