Skip to content

Commit 5a2c823

Browse files
committed
Updated README. Simple modifies.
1 parent 4a8621e commit 5a2c823

File tree

7 files changed

+219
-147
lines changed

7 files changed

+219
-147
lines changed
File renamed without changes.

README.md

Lines changed: 111 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
# Laravel Repositories
22

3-
Laravel Repositories to abstract the database layer.
3+
[![Latest Stable Version](https://poser.pugx.org/freevital/laravel-repository/v/stable)](https://packagist.org/packages/freevital/laravel-repository)
4+
[![Total Downloads](https://poser.pugx.org/freevital/laravel-repository/downloads)](https://packagist.org/packages/freevital/laravel-repository)
5+
[![Monthly Downloads](https://poser.pugx.org/freevital/laravel-repository/d/monthly)](https://packagist.org/packages/freevital/laravel-repository)
6+
[![License](https://poser.pugx.org/freevital/laravel-repository/license)](https://packagist.org/packages/freevital/laravel-repository)
47

5-
##Installation
8+
Laravel Repositories to abstract a database layer.
69

7-
Run the following command to install the latest version
10+
## Installation
11+
12+
Run the following command to install the latest version:
813

914
```bash
1015
composer require "freevital/laravel-repository"
1116
```
1217

13-
##Usage
18+
## Usage
1419

15-
###Create a Repository
20+
### Create a Repository
1621

1722
Your repository class must extend `Freevital\Repository\Eloquent\BaseRepository` abstract class and implement method `model()` which returns model's class name.
1823

@@ -35,7 +40,7 @@ class PostRepository extends BaseRepository
3540
}
3641
```
3742

38-
###Use the repository in the controller
43+
### Use Repository in the Controller
3944

4045
```php
4146
namespace App\Http\Controllers;
@@ -74,7 +79,7 @@ class PostController extends Controller
7479
}
7580
```
7681

77-
###Create a Repository Criteria
82+
### Create a Repository Criteria
7883

7984
Optionally you may create a separate Criteria class to apply specific query conditions. Your Criteria class must implement `Freevital\Repository\Contracts\CriteriaContract` interface.
8085

@@ -90,7 +95,7 @@ class BySlugCriteria implements CriteriaContract
9095
/**
9196
* @var string
9297
*/
93-
protected $title;
98+
protected $slug;
9499

95100
/**
96101
* @param string $slug
@@ -110,14 +115,14 @@ class BySlugCriteria implements CriteriaContract
110115
*/
111116
public function apply(Builder $query, RepositoryContract $repository)
112117
{
113-
return $query->where('name', $this->slug);
118+
return $query->where('slug', $this->slug);
114119
}
115120
}
116121
```
117122

118-
###Use Repository Criteria in the controller
123+
### Use Repository Criteria in the Controller
119124

120-
You may use multiple criteria in a repository.
125+
You may use multiple criteria in the repository.
121126

122127
```php
123128
namespace App\Http\Controllers;
@@ -139,7 +144,7 @@ class PostController extends Controller
139144
public function show($slug)
140145
{
141146
$post = $this->postRepository
142-
->pushCriteria(new WithCommentsCriteria($slug))
147+
->pushCriteria(new WithCommentsCriteria())
143148
->pushCriteria(new BySlugCriteria($slug))
144149
->first();
145150

@@ -148,9 +153,43 @@ class PostController extends Controller
148153
}
149154
```
150155

151-
##Available Methods
156+
## Criteria Macros
157+
152158

153-
###Freevital\Repository\Contracts\RepositoryContract
159+
If you would like to extend the repository functionality with custom common scope (ex. ActiveCriteria), you may use BaseRepository's macro method. For example, from a service provider's boot method:
160+
161+
```php
162+
namespace App\Providers;
163+
164+
use Freevital\Repository\Criteria\ActiveCriteria;
165+
use Freevital\Repository\Eloquent\BaseRepository;
166+
use Illuminate\Support\ServiceProvider;
167+
168+
class RepositoryMacroServiceProvider extends ServiceProvider
169+
{
170+
/**
171+
* Register the application's repository macros.
172+
*
173+
* @return void
174+
*/
175+
public function boot()
176+
{
177+
BaseRepository::macro('active', function (BaseRepository $repository) {
178+
$repository->pushCriteria(new ActiveCriteria());
179+
});
180+
}
181+
}
182+
```
183+
184+
The macro function accepts a name as its first argument, and a Closure as its second. The macro's Closure will be executed when calling the macro name from any Repository instance:
185+
186+
```php
187+
$this->postRepository->active()->all();
188+
```
189+
190+
## Available Methods
191+
192+
#### Freevital\Repository\Contracts\RepositoryContract
154193

155194
```php
156195
paginate($limit = null, $columns = ['*'], $method = 'paginate')
@@ -182,7 +221,7 @@ scopeQuery(\Closure $scope)
182221
resetScope()
183222
```
184223

185-
###Freevital\Repository\Contracts\RepositoryCriteriaContract
224+
#### Freevital\Repository\Contracts\RepositoryCriteriaContract
186225

187226
```php
188227
pushCriteria($criteria)
@@ -193,16 +232,70 @@ skipCriteria($status = true)
193232
resetCriteria()
194233
```
195234

196-
###Freevital\Repository\Contracts\CriteriaContract
235+
#### Freevital\Repository\Contracts\CriteriaContract
197236

198237
```php
199238
apply(Builder $query, RepositoryContract $repository)
200239
```
201240

202-
##Credits
241+
## Example usage
242+
243+
Get all entities:
244+
245+
```php
246+
$this->postRepository->all();
247+
248+
// Fetch the specific columns
249+
$this->postRepository->all(['id', 'title']);
250+
```
251+
252+
Entity pagination:
253+
254+
```php
255+
$this->postRepository->paginate(20);
256+
```
257+
258+
Get an entity by id:
259+
260+
```php
261+
$this->postRepository->find($id);
262+
```
263+
264+
Get first entity:
265+
266+
```php
267+
$this->postRepository->pushCriteria(...)->first();
268+
```
269+
270+
Get entities count:
271+
272+
```php
273+
$this->postRepository->pushCriteria(...)->count();
274+
```
275+
276+
Create new entity:
277+
278+
```php
279+
$this->postRepository->create(Input::all());
280+
```
281+
282+
Update an entity by id:
283+
284+
```php
285+
$this->postRepository->update(Input::all(), $id);
286+
```
287+
288+
Delete or force delete an entity by id:
289+
290+
```php
291+
$this->postRepository->delete($id);
292+
$this->postRepository->forceDelete($id);
293+
```
294+
295+
## Credits
203296

204297
This package in mainly based on package by [@andersao](https://github.com/andersao/l5-repository).
205298

206-
##License
299+
## License
207300

208301
The contents of this repository is released under the MIT license.

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"support": {
1212
"email": "[email protected]",
1313
"issues":"https://github.com/freevital/laravel-repository/issues",
14-
"wiki":"https://github.com/freevital/laravel-repository",
1514
"source":"https://github.com/freevital/laravel-repository"
1615
},
1716
"type": "library",

0 commit comments

Comments
 (0)