Skip to content

Commit 88b6ff0

Browse files
committed
Update README.md to match new version
1 parent 9606072 commit 88b6ff0

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

README.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,43 @@ use PPSpaces\Repositories\Model as Repository;
9797

9898
class UserRepository extends Repository {
9999

100-
protected $user;
101-
102-
public function __construct(User $user) {
103-
$this->user = $user;
100+
/**
101+
* The user model instance.
102+
*
103+
* @var \App\User
104+
*/
105+
protected $model = "App\User";
106+
107+
/**
108+
* Scope a query for the model before executing
109+
*
110+
* @param \Illuminate\Database\Query\Builder $query
111+
* @return void
112+
*/
113+
public function before($query) {
114+
$query->role('staff');
104115
}
105116

117+
/**
118+
* Get all of the models from the database.
119+
*
120+
* @param array|mixed $columns
121+
* @return \Illuminate\Database\Eloquent\Collection|static[]
122+
*/
106123
public function all($columns = ['*']) {
107-
return $this->user
108-
->role('staff')
109-
->paginate();
124+
$users = $this->repository
125+
->active()
126+
->orderBy('updated_at', 'DESC')
127+
->get();
128+
129+
return $users;
110130
}
111131
}
112132
```
113133

114134
> NOTE: Check `PPSpaces\Repositories\Model` for available methods that you may override. Keep in mind that you still have access to all Model instance that you've created. The `$this->user` is the instance of your `\App\User` model.
115135
116-
Within your `UserController` assume you have a resource controller created. Inject the `UserRepository` to the contoller.
136+
Within your `UserController` assume you have a resource controller created. Inject the `UserRepository` to the contoller. Now you can access the repository in your controller method:
117137

118138
```php
119139
use App\Http\Repositories\UserRepository;
@@ -126,15 +146,31 @@ class UserController extends Controller
126146
{
127147
$this->users = $users;
128148
}
149+
150+
public function index()
151+
{
152+
return $this->users->all();
153+
}
129154
}
130155
```
131156

132-
Now you can access the repository in your controller method:
157+
Or alternatively, you may use **Route Model Binding** on the controller actions whose `type-hinted` variable names match a route segment name.
158+
159+
> Read more about [Route Model Binding](https://laravel.com/docs/master/routing#route-model-binding) here
133160
134161
```php
135-
public function index()
162+
public function index(UserRepository $user)
136163
{
137-
return $this->users->all();
164+
return $user->all();
165+
}
166+
167+
public function show(UserRepository $user)
168+
{
169+
// Authorizing the repository model
170+
// Check https://laravel.com/docs/master/authorization
171+
$this->authorize('view', $user->model());
172+
173+
return $user->all();
138174
}
139175
```
140176

0 commit comments

Comments
 (0)