File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,63 @@ php artisan make:repository UserRepository --model=User
7575 +-------------+ +-------------+ +-------------+
7676```
7777
78+ ### How to use Repository
79+
80+ Create your repository, e.g. ` UserRepository ` for ` User ` model:
81+
82+ ``` sh
83+ php artisan make:repository UserRepository --model=User
84+ ```
85+
86+ Update ` UserRepository ` logic:
87+
88+ ``` php
89+ namespace App\Repositories;
90+
91+ use App\User;
92+ use PPSpaces\Repositories\Model as Repository;
93+
94+ class UserRepository extends Repository {
95+
96+ public function __construct(User $user) {
97+ $this->model = $user;
98+ }
99+
100+ public function all($columns = ['*']) {
101+ return $this->model
102+ ->role('staff')
103+ ->paginate();
104+ }
105+ }
106+ ```
107+
108+ > NOTE: Check ` PPSpaces\Repositories\Model ` for available methods that you may override.
109+
110+ Within your ` UserController ` assume you have a resource controller created. Inject the ` UserRepository ` to the contoller.
111+
112+ ``` php
113+ use App\Repositories\UserRepository;
114+
115+ class UserController extends Controller
116+ {
117+ protected $users;
118+
119+ public function __construct(UserRepository $users)
120+ {
121+ $this->users = $users;
122+ }
123+ }
124+ ```
125+
126+ Now you can access the repository in your controller method:
127+
128+ ``` php
129+ public function index()
130+ {
131+ return $this->users->all();
132+ }
133+ ```
134+
78135## Help
79136
80137```
You can’t perform that action at this time.
0 commit comments