Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork22
🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.
License
awes-io/repository
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.
Via Composer
$ composer require awes-io/repository
The package will automatically register itself.
First publish config:
php artisan vendor:publish --provider="AwesIO\Repository\RepositoryServiceProvider" --tag="config"
// $repository->smartPaginate() related parameters'smart_paginate' => [// name of request parameter to take paginate by value from'request_parameter' =>'limit',// default paginate by value'default_limit' =>15,// max paginate by value'max_limit' =>100,]
https://example.com/news?title=Title&custom=value&orderBy=name_descIt will automatically apply built-in constraints onto the query as well as any custom scopes and criteria you need:
protected$searchable = [// where 'title' equals 'Title''title',];protected$scopes = [// and custom parameter used in your scope'custom' => MyScope::class,];
class MyScopeextends ScopeAbstract{publicfunctionscope($builder,$value,$scope) {return$builder->where($scope,$value)->orWhere(...); }}
Ordering by any field is available:
protected$scopes = [// orderBy field'orderBy' => OrderByScope::class,];
Package can also apply any custom criteria:
return$this->news->withCriteria([newMyCriteria(['category_id' =>'1','name' =>'Name' ]) ...])->get();
Create your model:
namespaceApp;useIlluminate\Database\Eloquent\Model;class Newsextends Model { ...}
Extend it fromAwesIO\Repository\Eloquent\BaseRepository and provideentity() method to return full model class name:
namespaceApp;useAwesIO\Repository\Eloquent\BaseRepository;class NewsRepositoryextends BaseRepository{publicfunctionentity() {return News::class; }}
useApp\NewsRepository;class NewsControllerextends BaseController {protected$news;publicfunction__construct(NewsRepository$news) {$this->news =$news; }....}
Execute the query as a "select" statement or get all results:
$news =$this->news->get();
Execute the query and get the first result:
$news =$this->news->first();
Find a model by its primary key:
$news =$this->news->find(1);
Add basic where clauses and execute the query:
$news =$this->news->->findWhere([// where id equals 1'id' =>'1',// other "where" operations ['news_category_id','<','3'], ... ]);
Paginate the given query:
$news =$this->news->paginate(15);
Paginate the given query into a simple paginator:
$news =$this->news->simplePaginate(15);
Paginate the given query by 'limit' request parameter:
$news =$this->news->smartPaginate();
Add an "order by" clause to the query:
$news =$this->news->orderBy('title','desc')->get();
Save a new model and return the instance:
$news =$this->news->create($request->all());
Update a record:
$this->news->update($request->all(),$id);
Delete a record by id:
$this->news->destroy($id);
Attach models to the parent:
$this->news->attach($parentId,$relationship,$idsToAttach);
Detach models from the relationship:
$this->news->detach($parentId,$relationship,$idsToDetach);
Find model or throw an exception if not found:
$this->news->findOrFail($id);
Execute the query and get the first result or throw an exception:
$this->news->firstOrFail();
Criteria are a way to build up specific query conditions.
useAwesIO\Repository\Contracts\CriterionInterface;class MyCriteriaimplements CriterionInterface {protected$conditions;publicfunction__construct(array$conditions) {$this->conditions =$conditions; }publicfunctionapply($entity) {foreach ($this->conditionsas$field =>$value) {$entity =$entity->where($field,'=',$value); }return$entity; }}
Multiple Criteria can be applied:
useApp\NewsRepository;class NewsControllerextends BaseController {protected$news;publicfunction__construct(NewsRepository$news) {$this->news =$news; }publicfunctionindex() {return$this->news->withCriteria([newMyCriteria(['category_id' =>'1','name' =>'Name' ]),newWhereAdmin(), ... ])->get(); }}
In your repository define which fields can be used to scope your queries by setting$searchable property.
protected$searchable = [// where 'title' equals parameter value'title',// orWhere equals'body' =>'or',// where like'author' =>'like',// orWhere like'email' =>'orLike',];
Search by searchables:
publicfunctionindex($request){return$this->news->scope($request)->get();}
https://example.com/news?title=Title&body=Text&author=&email=gmailAlso several serchables enabled by default:
protected$scopes = [// orderBy field'orderBy' => OrderByScope::class,// where created_at date is after'begin' => WhereDateGreaterScope::class,// where created_at date is before'end' => WhereDateLessScope::class,];
$this->news->scope($request)->get();
Enable ordering for specific fields by adding$orderable property to your model class:
public$orderable = ['email'];
https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26orderBy=email_desc will order by email in descending order,orderBy=email - in ascending
You can also build your own custom scopes. In your repository overridescope() method:
publicfunctionscope($request){// apply build-in scopesparent::scope($request);// apply custom scopes$this->entity = (newNewsScopes($request))->scope($this->entity);return$this;}
Create yourscopes class and extendScopesAbstract
useAwesIO\Repository\Scopes\ScopesAbstract;class NewsScopesextends ScopesAbstract{protected$scopes = [// here you can add field-scope mappings'field' => MyScope::class, ];}
Now you can build any scopes you need:
useAwesIO\Repository\Scopes\ScopeAbstract;class MyScopeextends ScopeAbstract{publicfunctionscope($builder,$value,$scope) {return$builder->where($scope,$value); }}
Package provides useful artisan command:
php artisan repository:generate Models/Order --scope=Search
Main repository:App\Repositories\Orders\OrdersRepository
Main scopes class:App\Repositories\Orders\Scopes\OrdersScopes
Individual search scope class:App\Repositories\Orders\Scopes\SearchOrdersScope
The coverage of the package is.
You can run the tests with:
composertestPlease seecontributing.md for details and a todolist.
About
🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.
