You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel Repositories to abstract a database layer.
6
9
7
-
Run the following command to install the latest version
10
+
## Installation
11
+
12
+
Run the following command to install the latest version:
8
13
9
14
```bash
10
15
composer require "freevital/laravel-repository"
11
16
```
12
17
13
-
##Usage
18
+
##Usage
14
19
15
-
###Create a Repository
20
+
###Create a Repository
16
21
17
22
Your repository class must extend `Freevital\Repository\Eloquent\BaseRepository` abstract class and implement method `model()` which returns model's class name.
18
23
@@ -35,7 +40,7 @@ class PostRepository extends BaseRepository
35
40
}
36
41
```
37
42
38
-
###Use the repository in the controller
43
+
###Use Repository in the Controller
39
44
40
45
```php
41
46
namespace App\Http\Controllers;
@@ -74,7 +79,7 @@ class PostController extends Controller
74
79
}
75
80
```
76
81
77
-
###Create a Repository Criteria
82
+
###Create a Repository Criteria
78
83
79
84
Optionally you may create a separate Criteria class to apply specific query conditions. Your Criteria class must implement `Freevital\Repository\Contracts\CriteriaContract` interface.
80
85
@@ -90,7 +95,7 @@ class BySlugCriteria implements CriteriaContract
90
95
/**
91
96
* @var string
92
97
*/
93
-
protected $title;
98
+
protected $slug;
94
99
95
100
/**
96
101
* @param string $slug
@@ -110,14 +115,14 @@ class BySlugCriteria implements CriteriaContract
110
115
*/
111
116
public function apply(Builder $query, RepositoryContract $repository)
112
117
{
113
-
return $query->where('name', $this->slug);
118
+
return $query->where('slug', $this->slug);
114
119
}
115
120
}
116
121
```
117
122
118
-
###Use Repository Criteria in the controller
123
+
###Use Repository Criteria in the Controller
119
124
120
-
You may use multiple criteria in a repository.
125
+
You may use multiple criteria in the repository.
121
126
122
127
```php
123
128
namespace App\Http\Controllers;
@@ -139,7 +144,7 @@ class PostController extends Controller
139
144
public function show($slug)
140
145
{
141
146
$post = $this->postRepository
142
-
->pushCriteria(new WithCommentsCriteria($slug))
147
+
->pushCriteria(new WithCommentsCriteria())
143
148
->pushCriteria(new BySlugCriteria($slug))
144
149
->first();
145
150
@@ -148,9 +153,43 @@ class PostController extends Controller
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:
0 commit comments