prettus / laravel-repository
Repositories to abstract the database layer
Installs: 17 462
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: >=5.4.0
- prettus/laravel-validation: 1.0.*
This package is auto-updated.
Last update: 2019-09-03 21:03:18 UTC
README
###This package is abandoned and no longer maintained. The author suggests using the https://github.com/andersao/l5-repository package instead.
Laravel 4 - Repositories
Laravel 4 Repositories is used to abstract the data layer, making our application more flexible to maintain.
Installation
Add this line "prettus/laravel-repository": "2.0.*" in your composer.json.
"require": { "prettus/laravel-repository": "2.0.*" }
Issue composer update
Add to app/config/app.php service provider array:
'Prettus\Repository\RepositoryServiceProvider',
Publish Configuration
php artisan config:publish prettus/laravel-repository
Methods
Repository
- scopeReset()
- find($id, $columns = ['*'])
- findByField($field, $value, $columns = ['*'])
- all($columns = array('*'))
- paginate($limit = null, $columns = ['*'])
- create(array $attributes)
- update(array $attributes, $id)
- delete($id)
- getModel()
- with(array $relations);
- pushCriteria(Criteria $criteria)
- getCriteria()
- getByCriteria(Criteria $criteria)
- skipCriteria()
Criteria
- apply($query)
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.
class Post extends Eloquent { // or Ardent, Or any other Model Class protected $fillable = [ 'title', 'author', ... ]; ... }
Create a Repository
use Prettus\Repository\Eloquent\Repository; class PostRepository extends Repository { public function __construct(Post $model) { parent::__construct($model); } }
Use methods
class PostsController extends BaseController { /** * @var PostRepository */ protected $repository; public function __construct(PostRepository $repository){ $this->repository = $repository; } .... }
Find all results in Repository
$posts = $this->repository->all();
Find all results in Repository with pagination
$posts = $this->repository->paginate($limit = null, $columns = ['*']);
Find by result by id
$post = $this->repository->find($id);
Create new entry in Repository
$post = $this->repository->create( Input::all() );
Update entry in Repository
$post = $this->repository->update( Input::all(), $id );
Delete entry in Repository
$this->repository->delete($id)
Create a Criteria
Criteria is a way to change the repository of the query by applying specific conditions according to their need . You can add multiple Criteria in your repository
class MyCriteria implements \Prettus\Repository\Contracts\Criteria { public function apply($query) { $query = $query->where('user_id','=', Auth::user()->id ); return $query; } }
Using the Criteria in a Controller
class PostsController extends BaseController { /** * @var PostRepository */ protected $repository; public function __construct(PostRepository $repository){ $this->repository = $repository; } public function index() { $this->repository->pushCriteria(new MyCriteria()); $posts = $this->repository->all(); ... } }
Getting results from criteria
$posts = $this->repository->getByCriteria(new MyCriteria());
Setting Criteria default in Repository
use Prettus\Repository\Eloquent\Repository; class PostRepository extends Repository { public function __construct(Post $model) { parent::__construct($model); } public function boot(){ $this->pushCriteria(new MyCriteria()); $this->pushCriteria(new AnotherCriteria()); ... } }
Skip criteria defined in the repository
Use skipCriteria before any method in the repository
$posts = $this->repository->skipCriteria()->all();
Using the RequestCriteria
RequestCriteria is a standard Criteria implementation. It enables filters perform in the repository from parameters sent in the request.
You can perform a dynamic search , filtering the data and customize 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
use Prettus\Repository\Eloquent\Repository; use Prettus\Repository\Criteria\RequestCriteria; class PostRepository extends Repository { /** * @var array */ protected $fieldSearchable = [ 'name', 'email' ]; public function __construct(Post $model) { parent::__construct($model); } public function boot(){ $this->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria')); ... } }
Remember, you need to define which fields from the model can are searchable.
In your repository set $fieldSearchable with their fields searchable.
protected $fieldSearchable = [ 'name', 'email' ];
You can set the type of condition will be used to perform the query , the default condition is "="
protected $fieldSearchable = [ 'name'=>'like', 'email', // Default Condition "=" 'your_field'=>'condition' ];
####Enabling in your Controller
public function index() { $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria')); $posts = $this->repository->all(); ... }
Example the Crirteria
Request all data without filter by request
[ { "id": 1, "name": "Anderson Andrade", "email": "email@gmail.com", "created_at": "-0001-11-30 00:00:00", "updated_at": "-0001-11-30 00:00:00" }, { "id": 2, "name": "Lorem Ipsum", "email": "lorem@ipsum.com", "created_at": "-0001-11-30 00:00:00", "updated_at": "-0001-11-30 00:00:00" }, { "id": 3, "name": "Laravel", "email": "laravel@gmail.com", "created_at": "-0001-11-30 00:00:00", "updated_at": "-0001-11-30 00:00:00" } ]
Conducting research in the repository
http://prettus.local/users?search=Anderson%20Andrade
or
http://prettus.local/users?search=Anderson&searchFields=name:like
or
http://prettus.local/users?search=email@gmail.com&searchFields=email:=
[ { "id": 1, "name": "Anderson Andrade", "email": "email@gmail.com", "created_at": "-0001-11-30 00:00:00", "updated_at": "-0001-11-30 00:00:00" } ]
Filtering fields
http://prettus.local/users?filter=id;name
[ { "id": 1, "name": "Anderson Andrade" }, { "id": 2, "name": "Lorem Ipsum" }, { "id": 3, "name": "Laravel" } ]
Sorting the results
http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc
[ { "id": 3, "name": "Laravel" }, { "id": 2, "name": "Lorem Ipsum" }, { "id": 1, "name": "Anderson Andrade" } ]
####Overwrite params name
You can change the name of the parameters in the configuration file config/repository-criteria.php
Author
Anderson Andrade - contato@andersonandra.de