Download the PHP package nascentafrica/eloquent-repository without Composer
On this page you can find all versions of the php package nascentafrica/eloquent-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nascentafrica/eloquent-repository
More information about nascentafrica/eloquent-repository
Files in nascentafrica/eloquent-repository
Package eloquent-repository
Short Description A repository package for Laravel models.
License MIT
Informations about the package eloquent-repository
Eloquent Repository
Eloquent Repository is used to abstract the data layer, making our application more flexible to maintain.
Many thanks to Anderson Andrade for this project is a stripped down version of andersao/l5-repository. This was done because i preferred to do my validations and transformations else where and not in my repository.
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 NascentAfrica\EloquentRepository\RepositoryServiceProvider::class
to the end of the providers
array:
If Lumen
Publish Configuration
Methods
NascentAfrica\EloquentRepository\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 = [*])
- count()
- countWhere(array $where = [], $columns = '*'): int
- create(array $attributes)
- update(array $attributes, $id)
- updateOrCreate(array $attributes, array $values = [])
- delete($id)
- deleteWhere(array $where)
- orderBy($column, $direction = 'asc');
- onlyTrashed()
- pluck($column, $key = null)
- with(array $relations)
- has(string $relation)
- whereHas(string $relation, closure $closure)
- function withTrashed()
- hidden(array $fields)
- visible(array $fields)
- search($search = '', $callback = null)
- scopeQuery(Closure $scope)
- getFieldsSearchable()
- setPresenter($presenter)
- skipPresenter($status = true)
NascentAfrica\EloquentRepository\Contracts\RepositoryCriteriaInterface
- pushCriteria($criteria)
- popCriteria($criteria)
- getCriteria()
- getByCriteria(CriteriaInterface $criteria)
- skipCriteria($status = true)
- getFieldsSearchable()
NascentAfrica\EloquentRepository\Contracts\CacheableInterface
- setCacheRepository(CacheRepository $repository)
- getCacheRepository()
- getCacheKey($method, $args = null)
- getCacheMinutes()
- skipCache($status = true)
NascentAfrica\EloquentRepository\Contracts\CriteriaInterface
- apply($model, RepositoryInterface $repository);
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 namespaces
array are actually used as both namespace and file paths.
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 Model if it does not exist, the Repository and the interface classes.
It will also create a new RepositoryServiceProvider
if it does not exist 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:
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
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://localhost/users
Conducting research in the repository
http://localhost/users?search=John%20Doe
or
http://localhost/users?search=John&searchFields=name:like
or
http://localhost/[email protected]&searchFields=email:=
or
http://localhost/users?search=name:John Doe;email:[email protected]
or
http://localhost/users?search=name:John;email:[email protected]&searchFields=name:like;email:=
By default RequestCriteria makes its queries using the OR comparison operator for each query parameter.
http://localhost/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://localhost/users?search=age:17;email:[email protected]&searchJoin=and
Filtering fields
http://localhost/users?filter=id;name
Sorting the results
http://localhost/users?filter=id;name&orderBy=id&sortedBy=desc
Sorting through related tables
http://localhost/users?orderBy=posts|title&sortedBy=desc
Query will have something like this
http://localhost/users?orderBy=posts:custom_id|posts.title&sortedBy=desc
Query will have something like this
Add relationship
http://localhost/users?with=groups
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
License
This software is released under The MIT License (MIT).
All versions of eloquent-repository with dependencies
illuminate/http Version ~5.0|~6.0|~7.0
illuminate/config Version ~5.0|~6.0|~7.0
illuminate/support Version ~5.0|~6.0|~7.0
illuminate/database Version ~5.0|~6.0|~7.0
illuminate/pagination Version ~5.0|~6.0|~7.0
illuminate/console Version ~5.0|~6.0|~7.0
illuminate/filesystem Version ~5.0|~6.0|~7.0