This package is abandoned and no longer maintained. The author suggests using the https://github.com/andersao/l5-repository package instead.
Laravel 4 - Repositories
, (*1)
Laravel 4 Repositories is used to abstract the data layer, making our application more flexible to maintain., (*2)
See Laravel 5 - Repositories, (*3)
Installation
Add this line "prettus/laravel-repository": "2.0.*" in your composer.json., (*4)
"require": {
"prettus/laravel-repository": "2.0.*"
}
Issue composer update, (*5)
Add to app/config/app.php service provider array:, (*6)
'Prettus\Repository\RepositoryServiceProvider',
Publish Configuration, (*7)
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
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., (*8)
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, (*9)
$posts = $this->repository->all();
Find all results in Repository with pagination, (*10)
$posts = $this->repository->paginate($limit = null, $columns = ['*']);
Find by result by id, (*11)
$post = $this->repository->find($id);
Create new entry in Repository, (*12)
$post = $this->repository->create( Input::all() );
Update entry in Repository, (*13)
$post = $this->repository->update( Input::all(), $id );
Delete entry in Repository, (*14)
$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, (*15)
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, (*16)
$posts = $this->repository->getByCriteria(new MyCriteria());
Setting Criteria default in Repository, (*17)
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, (*18)
$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., (*19)
You can perform a dynamic search , filtering the data and customize queries, (*20)
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, (*21)
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., (*22)
In your repository set $fieldSearchable with their fields searchable., (*23)
protected $fieldSearchable = [
'name',
'email'
];
You can set the type of condition will be used to perform the query , the default condition is "=", (*24)
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, (*25)
http://prettus.local/users, (*26)
[
{
"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, (*27)
http://prettus.local/users?search=Anderson%20Andrade, (*28)
or, (*29)
http://prettus.local/users?search=Anderson&searchFields=name:like, (*30)
or, (*31)
http://prettus.local/users?search=email@gmail.com&searchFields=email:=, (*32)
[
{
"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, (*33)
http://prettus.local/users?filter=id;name, (*34)
[
{
"id": 1,
"name": "Anderson Andrade"
},
{
"id": 2,
"name": "Lorem Ipsum"
},
{
"id": 3,
"name": "Laravel"
}
]
Sorting the results, (*35)
http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc, (*36)
[
{
"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, (*37)
Author
Anderson Andrade - contato@andersonandra.de, (*38)