Download the PHP package gigabooksite/l5-repository without Composer
On this page you can find all versions of the php package gigabooksite/l5-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gigabooksite/l5-repository
More information about gigabooksite/l5-repository
Files in gigabooksite/l5-repository
Package l5-repository
Short Description Laravel 5|6|7|8|9|10 - Repositories to the database layer
License MIT
Homepage http://andersao.github.io/l5-repository
Informations about the package l5-repository
Laravel 5 Repositories
Laravel 5 Repositories is used to abstract the data layer, making our application more flexible to maintain.
See versions: 1.0.* / 2.0.*
Migrate to: 2.1
You want to know a little more about the Repository pattern? Read this great article.
Table of Contents
- Installation
- Composer
- Laravel
- Methods
- RepositoryInterface
- RepositoryCriteriaInterface
- CacheableInterface
- PresenterInterface
- CriteriaInterface
- Usage
- Create a Model
- Create a Repository
- Generators
- Use methods
- Create a Criteria
- Using the Criteria in a Controller
- Using the RequestCriteria
- Cache
- Usage
- Config
- Validators
- Using a Validator Class
- Create a Validator
- Enabling Validator in your Repository
- Defining rules in the repository
- Using a Validator Class
- Presenters
- Fractal Presenter
- Create a Fractal Presenter
- Model Transformable
- Enabling in your Repository
- Fractal Presenter
Installation
Composer
Execute the following command to get the latest version of the package:
Laravel
>= laravel5.5
ServiceProvider will be attached automatically
Other
In your config/app.php
add Prettus\Repository\Providers\RepositoryServiceProvider::class
to the end of the providers
array:
If Lumen
Publish Configuration
Methods
Prettus\Repository\Contracts\RepositoryInterface
- all($columns = array('*'))
- first($columns = array('*'))
- paginate($limit = null, $columns = ['*'])
- find($id, $columns = ['*'])
- findByField($field, $value, $columns = ['*'])
- findWhere(array $where, $columns = ['*'])
- findWhereIn($field, array $where, $columns = [*])
- findWhereNotIn($field, array $where, $columns = [*])
- findWhereBetween($field, array $where, $columns = [*])
- create(array $attributes)
- update(array $attributes, $id)
- updateOrCreate(array $attributes, array $values = [])
- delete($id)
- deleteWhere(array $where)
- orderBy($column, $direction = 'asc');
- with(array $relations);
- has(string $relation);
- whereHas(string $relation, closure $closure);
- hidden(array $fields);
- visible(array $fields);
- scopeQuery(Closure $scope);
- getFieldsSearchable();
- setPresenter($presenter);
- skipPresenter($status = true);
Prettus\Repository\Contracts\RepositoryCriteriaInterface
- pushCriteria($criteria)
- popCriteria($criteria)
- getCriteria()
- getByCriteria(CriteriaInterface $criteria)
- skipCriteria($status = true)
- getFieldsSearchable()
Prettus\Repository\Contracts\CacheableInterface
- setCacheRepository(CacheRepository $repository)
- getCacheRepository()
- getCacheKey($method, $args = null)
- getCacheTime()
- skipCache($status = true)
Prettus\Repository\Contracts\PresenterInterface
- present($data);
Prettus\Repository\Contracts\Presentable
- setPresenter(PresenterInterface $presenter);
- presenter();
Prettus\Repository\Contracts\CriteriaInterface
- apply($model, RepositoryInterface $repository);
Prettus\Repository\Contracts\Transformable
- transform();
Usage
Create a Model
Create your model normally, but it is important to define the attributes that can be filled from the input form data.
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.
You may want to save the root of your project folder out of the app and add another namespace, for example
Additionally, you may wish to customize where your generated classes end up being saved. That can be accomplished by editing the paths
node to your liking. For example:
Commands
To generate everything you need for your Model, run this command:
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:
You can also pass the options from the command, since this command is just a wrapper.
To generate a repository for your Post model, use the following command
To generate a repository for your Post model with Blog namespace, use the following command
Added fields that are fillable
To add validations rules directly with your command you need to pass the --rules
option and create migrations as well:
The command will also create your basic RESTfull controller so just add this line into your routes.php
file and you will have a basic CRUD:
When running the command, you will be creating the "Entities" folder and "Repositories" inside the folder that you set as the default.
Now that is done, you still need to bind its interface for your real repository, for example in your own Repositories Service Provider.
And use
Alternatively, you could use the artisan command to do the binding for you.
Use methods
Find all results in Repository
Find all results in Repository with pagination
Find by result by id
Hiding attributes of the model
Showing only specific attributes of the model
Loading the Model relationships
Find by result by field name
Find by result by multiple fields
Find by result by multiple values in one field
Find by result by excluding multiple values in one field
Find all using custom scope
Create new entry in Repository
Update entry in Repository
Delete entry in Repository
Delete entry in Repository by multiple fields
Create a Criteria
Using the command
Criteria are a way to change the repository of the query by applying specific conditions according to your needs. You can add multiple Criteria in your repository.
Using the Criteria in a Controller
Getting results from Criteria
Setting the default Criteria in Repository
Skip criteria defined in the repository
Use skipCriteria
before any other chaining method
Popping criteria
Use popCriteria
to remove a criteria
Using the RequestCriteria
RequestCriteria is a standard Criteria implementation. It enables filters to perform in the repository from parameters sent in the request.
You can perform a dynamic search, filter the data and customize the queries.
To use the Criteria in your repository, you can add a new criteria in the boot method of your repository, or directly use in your controller, in order to filter out only a few requests.
Enabling in your Repository
Remember, you need to define which fields from the model can be searchable.
In your repository set $fieldSearchable with the name of the fields to be searchable or a relation to fields.
You can set the type of condition which will be used to perform the query, the default condition is "="
Enabling in your Controller
Example the Criteria
Request all data without filter by request
http://prettus.local/users
Conducting research in the repository
http://prettus.local/users?search=John%20Doe
or
http://prettus.local/users?search=John&searchFields=name:like
or
http://prettus.local/[email protected]&searchFields=email:=
or
http://prettus.local/users?search=name:John Doe;email:[email protected]
or
http://prettus.local/users?search=name:John;email:[email protected]&searchFields=name:like;email:=
You can use params "search" without full params "searchFields".
http://prettus.local/users?search=id:2;age:17;email:[email protected]&searchFields='id':=
By default RequestCriteria makes its queries using the OR comparison operator for each query parameter.
http://prettus.local/users?search=age:17;email:[email protected]
The above example will execute the following query:
In order for it to query using the AND, pass the searchJoin parameter as shown below:
http://prettus.local/users?search=age:17;email:[email protected]&searchJoin=and
Filtering fields
http://prettus.local/users?filter=id;name
Sorting the results
http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc
Sorting through related tables
http://prettus.local/users?orderBy=posts|title&sortedBy=desc
Query will have something like this
http://prettus.local/users?orderBy=posts:custom_id|posts.title&sortedBy=desc
Query will have something like this
http://prettus.local/users?orderBy=posts:custom_id,other_id|posts.title&sortedBy=desc
Query will have something like this
Sorting multiple columns same sortedBy
http://prettus.local/users?orderBy=name;created_at&sortedBy=desc
Result will have something like this
Sorting multiple columns difference sortedBy
http://prettus.local/users?orderBy=name;created_at&sortedBy=desc;asc
Result will have something like this
Add relationship
http://prettus.local/users?with=groups
Between filter
http://prettus.local/product?search=price:100,500&searchFields=price:between
Result will have something like this
WhereIn filter
http://prettus.local/product?search=price:300,500&searchFields=price:in
Result will have something like this
Overwrite params name
You can change the name of the parameters in the configuration file config/repository.php
Cache
Add a layer of cache easily to your repository
Cache Usage
Implements the interface CacheableInterface and use CacheableRepository Trait.
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
It is possible to override these settings directly in the repository.
The cacheable methods are : all, paginate, find, findByField, findWhere, getByCriteria
Validators
Requires prettus/laravel-validator. composer require prettus/laravel-validator
Easy validation with prettus/laravel-validator
Using a Validator Class
Create a Validator
In the example below, we define some rules for both creation and edition
To define specific rules, proceed as shown below:
Enabling Validator in your Repository
Defining rules in the repository
Alternatively, instead of using a class to define its validation rules, you can set your rules directly into the rules repository property, it will have the same effect as a Validation class.
Validation is now ready. In case of a failure an exception will be given of the type: Prettus\Validator\Exceptions\ValidatorException
Presenters
Presenters function as a wrapper and renderer for objects.
Fractal Presenter
Requires Fractal. composer require league/fractal
There are two ways to implement the Presenter, the first is creating a TransformerAbstract and set it using your Presenter class as described in the Create a Transformer Class.
The second way is to make your model implement the Transformable interface, and use the default Presenter ModelFractarPresenter, this will have the same effect.
Transformer Class
Create a Transformer using the command
This will generate the class beneath.
Create a Transformer Class
Create a Presenter using the command
The command will prompt you for creating a Transformer too if you haven't already.
Create a Presenter
Enabling in your Repository
Or enable it in your controller with
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
There, now you can submit your Model individually, See an example:
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:
Model Class
Implement Interface
Enabling in your Repository
Prettus\Repository\Presenter\ModelFractalPresenter
is a Presenter default for Models implementing Transformable
Or enable it in your controller with
Skip Presenter defined in the repository
Use skipPresenter before any other chaining method
or
All versions of l5-repository with dependencies
illuminate/config Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
illuminate/support Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
illuminate/database Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
illuminate/pagination Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
illuminate/console Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
illuminate/filesystem Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0
gigabooksite/laravel-validation Version ~1.1|~1.2|~1.3|~1.4|~1.5|~1.6|~2.0
illuminate/validation Version ~5.0|~6.0|~7.0|^8.0|^9.0|^10.0|^11.0