Skip to content

Commit 3af2289

Browse files
committed
Publishe papackage
0 parents  commit 3af2289

17 files changed

+2854
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
composer.phar
3+
vendor/

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
9+
before_script:
10+
- travis_retry composer self-update
11+
- travis_retry composer install --prefer-source --no-interaction --dev
12+
13+
script: phpunit

README.md

+357
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
# Laravel 5 Repositories
2+
3+
Laravel 5 Repositories is used to abstract the data layer, making our application more flexible to maintain.
4+
5+
## Installation
6+
7+
Add this line "prettus/l5-repository": "1.0.*" in your composer.json.
8+
9+
```json
10+
"require": {
11+
"prettus/l5-repository": "1.0.*"
12+
}
13+
```
14+
15+
Issue composer update
16+
17+
Add to app/config/app.php service provider array:
18+
19+
```php
20+
'Prettus\Repository\RepositoryServiceProvider',
21+
```
22+
23+
Publish Configuration
24+
25+
```shell
26+
php artisan vendor:publish --provider="Prettus\Repository\RepositoryServiceProvider"
27+
```
28+
29+
## Methods
30+
31+
### Repository
32+
33+
- scopeReset()
34+
- find($id, $columns = ['*'])
35+
- findByField($field, $value, $columns = ['*'])
36+
- all($columns = array('*'))
37+
- paginate($limit = null, $columns = ['*'])
38+
- create(array $attributes)
39+
- update(array $attributes, $id)
40+
- delete($id)
41+
- getModel()
42+
- with(array $relations);
43+
- pushCriteria(Criteria $criteria)
44+
- getCriteria()
45+
- getByCriteria(Criteria $criteria)
46+
- skipCriteria()
47+
48+
### Criteria
49+
50+
- apply($query)
51+
52+
## Utilisation
53+
54+
### Create a Repository
55+
56+
```php
57+
use Prettus\Repository\Eloquent\Repository;
58+
59+
class PostRepository extends RepositoryBase {
60+
61+
public function __construct(Post $model)
62+
{
63+
parent::__construct($model);
64+
}
65+
66+
}
67+
```
68+
69+
### Using the Repository in a Controller
70+
71+
```php
72+
73+
class PostsController extends BaseController {
74+
75+
/**
76+
* @var PostRepository
77+
*/
78+
protected $repository;
79+
80+
public function __construct(PostRepository $repository){
81+
$this->repository = $repository;
82+
}
83+
84+
85+
public function index()
86+
{
87+
$posts = $this->repository->all();
88+
...
89+
}
90+
91+
92+
public function show($id)
93+
{
94+
$post = $this->repository->find($id);
95+
...
96+
}
97+
98+
public function store()
99+
{
100+
101+
$post = $this->repository->create( Input::all() );
102+
...
103+
}
104+
105+
public function update($id)
106+
{
107+
$post = $this->repository->update( Input::all(), $id );
108+
...
109+
}
110+
111+
public function destroy($id){
112+
$this->repository->delete($id)
113+
...
114+
}
115+
}
116+
```
117+
118+
### Create a Criteria
119+
120+
```php
121+
class MyCriteria implements \Prettus\Repository\Contracts\Criteria {
122+
123+
public function apply($query)
124+
{
125+
$query = $query->where('user_id','=', Auth::user()->id );
126+
return $query;
127+
}
128+
}
129+
```
130+
131+
### Using the Criteria in a Controller
132+
133+
```php
134+
135+
class PostsController extends BaseController {
136+
137+
/**
138+
* @var PostRepository
139+
*/
140+
protected $repository;
141+
142+
public function __construct(PostRepository $repository){
143+
$this->repository = $repository;
144+
}
145+
146+
147+
public function index()
148+
{
149+
$this->repository->pushCriteria(new MyCriteria());
150+
$posts = $this->repository->all();
151+
...
152+
}
153+
154+
}
155+
```
156+
157+
Getting results from criteria
158+
159+
```php
160+
$posts = $this->repository->getByCriteria(new MyCriteria());
161+
```
162+
163+
Setting Criteria default in Repository
164+
165+
```php
166+
use Prettus\Repository\Eloquent\Repository;
167+
168+
class PostRepository extends RepositoryBase {
169+
170+
public function __construct(Post $model)
171+
{
172+
parent::__construct($model);
173+
}
174+
175+
public function boot(){
176+
$this->pushCriteria(new MyCriteria());
177+
$this->pushCriteria(new AnotherCriteria());
178+
...
179+
}
180+
181+
}
182+
```
183+
184+
### Using the RequestCriteria
185+
186+
RequestCriteria is a standard Criteria implementation. It enables filters perform in the repository from parameters sent in the request.
187+
188+
You can perform a dynamic search , filtering the data and customize queries
189+
190+
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
191+
192+
####Enabling in your Repository
193+
194+
```php
195+
use Prettus\Repository\Eloquent\Repository;
196+
use Prettus\Repository\Criteria\RequestCriteria;
197+
198+
class PostRepository extends RepositoryBase {
199+
200+
/**
201+
* @var array
202+
*/
203+
protected $fieldSearchable = [
204+
'name',
205+
'email'
206+
];
207+
208+
public function __construct(Post $model)
209+
{
210+
parent::__construct($model);
211+
}
212+
213+
public function boot(){
214+
$this->pushCriteria(new RequestCriteria(app('request')));
215+
...
216+
}
217+
218+
}
219+
```
220+
221+
Remember, you need to define which fields from the model can are searchable.
222+
223+
In your repository set **$fieldSearchable** with their fields searchable.
224+
225+
```php
226+
protected $fieldSearchable = [
227+
'name',
228+
'email'
229+
];
230+
```
231+
232+
You can set the type of condition will be used to perform the query , the default condition is "**=**"
233+
234+
```php
235+
protected $fieldSearchable = [
236+
'name'=>'like',
237+
'email', // Default Condition "**=**"
238+
'your_field'=>'condition'
239+
];
240+
```
241+
242+
####Enabling in your Controller
243+
244+
```php
245+
public function index()
246+
{
247+
$this->repository->pushCriteria(new RequestCriteria(app('request')));
248+
$posts = $this->repository->all();
249+
...
250+
}
251+
```
252+
253+
#### Example the Crirteria
254+
255+
Request all data without filter by request
256+
257+
*http://prettus.local/users*
258+
259+
```json
260+
[
261+
{
262+
id: 1,
263+
name: "Anderson Andrade",
264+
265+
created_at: "-0001-11-30 00:00:00",
266+
updated_at: "-0001-11-30 00:00:00"
267+
},
268+
{
269+
id: 2,
270+
name: "Lorem Ipsum",
271+
272+
created_at: "-0001-11-30 00:00:00",
273+
updated_at: "-0001-11-30 00:00:00"
274+
},
275+
{
276+
id: 3,
277+
name: "Laravel",
278+
279+
created_at: "-0001-11-30 00:00:00",
280+
updated_at: "-0001-11-30 00:00:00"
281+
}
282+
]
283+
``
284+
285+
Conducting research in the repository
286+
287+
*http://prettus.local/users?search=Anderson%20Andrade*
288+
289+
or
290+
291+
*http://prettus.local/users?search=Anderson&searchFields=name:like*
292+
293+
or
294+
295+
*http://prettus.local/[email protected]&searchFields=email:=*
296+
297+
```json
298+
[
299+
{
300+
id: 1,
301+
name: "Anderson Andrade",
302+
303+
created_at: "-0001-11-30 00:00:00",
304+
updated_at: "-0001-11-30 00:00:00"
305+
}
306+
]
307+
```
308+
309+
Filtering fields
310+
311+
*http://prettus.local/users?filter=id;name*
312+
313+
```json
314+
[
315+
{
316+
id: 1,
317+
name: "Anderson Andrade"
318+
},
319+
{
320+
id: 2,
321+
name: "Lorem Ipsum"
322+
},
323+
{
324+
id: 3,
325+
name: "Laravel"
326+
}
327+
]
328+
```
329+
330+
Sorting the results
331+
332+
*http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc*
333+
334+
```json
335+
[
336+
{
337+
id: 3,
338+
name: "Laravel"
339+
},
340+
{
341+
id: 2,
342+
name: "Lorem Ipsum"
343+
},
344+
{
345+
id: 1,
346+
name: "Anderson Andrade"
347+
}
348+
]
349+
```
350+
351+
####Overwrite params name
352+
353+
You can change the name of the parameters in the configuration file **config/repository-criteria.php**
354+
355+
# Author
356+
357+
Anderson Andrade - <[email protected]>

0 commit comments

Comments
 (0)