EXERCISE: Admin: users
Basic version
Preparation
- Create a new resource controller class UserController.php in the folder app/Http/Controllers/Admin:
php artisan make:controller Admin/UserController --model User
- Add the routes to (the admin group in) routes/web.php:
Route::resource('users', 'Admin\UserController');
VERB | URI | Controller Method | Comment |
---|---|---|---|
GET | admin/users | index | Display a listing of the resource |
GET | admin/users/create | create | Redirect to admin/users |
POST | admin/users | store | |
GET | admin/users/{user} | show | Redirect to admin/users |
GET | admin/users/{user}/edit | edit | Show the form for editing the specified resource |
PUT | admin/users/{user} | update | Update the specified resource in storage |
DELETE | admin/users/{user} | destroy | Remove the specified resource from storage |
Create new user
- This part is optional, because we already have a registration form that inserts a new user
Master page
- Show a table listing all the users (name, email, active, admin) with an edit and delete button
- Add some pagination to the page (we have more than 40 users)
- Add a basic form
- Text field to search for a user name OR his email
- Select list to sort on name (ascending or descending), email (ascending or descending), not active users or admin users
- Remember the old values of the form fields
- If no user is found, show an appropriate message to the user
Edit a user
- Create a new page with a form to update all database columns (except the password) for the user
- After a successful submission, redirect to the "original" (without filter and with the default sort method) master page and show a success alert at the top of the page
- Don't forget the server side validation!
- Remember the old values of the form fields
- Make sure that the old values of the checkboxes are correct!
REMARK
- The combination of
old()
and checkboxes is tricky, as no (old) value is passed when the checkbox is unchecked!- Suppose you want to disactivate a currently active user by unchecking the corresponding checkbox. If you then run into validation problems, the call
old('active', $user->active)
will tick the checkbox again (since there is no old value).
- Suppose you want to disactivate a currently active user by unchecking the corresponding checkbox. If you then run into validation problems, the call
- With
old('_token')
(exists if the form is under submission and isnull
when you visit the form for the first time) you can solve this, as suggested in this Laracasts discussion
Delete a user
- Ask for confirmation using a Noty confirm
- After deletion of the user, redirect to the "original" (without filter and with the default sort method) master page and show a success alert at the top of the page
REMARK
- It should not be possible to update your own profile through this user CRUD, as you could lock yourself out of (the admin section of) the application by removing your admin rights or deleting yourself
- Disable the edit and delete buttons on your personal row in the table on the master page (client-side)
- Implement some extra checks (server-side) that prevent updating or deleting your own profile
Example video of basic user CRUD
Advanced version
Preparation
- Create a new resource controller class User2Controller.php in the folder app/Http/Controllers/Admin:
php artisan make:controller Admin/User2Controller --model User
- Add the routes to routes/web.php:
Route::resource('users2', 'Admin\User2Controller', ['parameters' => ['users2' => 'user']]);
REMARK
- As explained in Admin: genres (advanced), Laravel is unable to determine the singularized version of the resource name
users2
, and therefore, the resulting routes will not work, unless we include['parameters' => ['users2' => 'user']]
as an extra argument in theRoute::resource()
command!
VERB | URI | Controller Method | Comment |
---|---|---|---|
GET | admin/users2 | index | Display a listing of the resource |
GET | admin/users2/create | create | Redirect to admin/users2 |
POST | admin/users2 | store | |
GET | admin/users2/{user} | show | Redirect to admin/users2 |
GET | admin/users2/{user}/edit | edit | Redirect to admin/users2 |
PUT | admin/users2/{user} | update | Update the specified resource in storage |
DELETE | admin/users2/{user} | destroy | Remove the specified resource from storage |
- Add a menu-item ' Users (advanced)' to the admin part of the navigation
Master page
- Similar to the basic version
REMARK
- Note that the table with all users should NOT be implemented with jQuery/AJAX (in contrast to what we did in Admin: genres (advanced) -> Master page with the controller method
qryGenres()
and the JavaScript functionloadTable()
), as it is quite difficult to combine such an approach with the search form/pagination on our master page
Edit a user
- Show a Bootstrap modal with a form to update all database columns (except the password) for the user
TIP
Use jQuery traversing techniques and CSS classes to "transfer" the data (name, email, ...) of a specific user (row) from the master page to the input fields of the bootstrap modal. This is easier than using data-attributes: as we won't reload the master page/user table after an update (further on), these data-attributes have to be adjusted as well in your script after a user update.
- Don't forget the server side validation!
- You can opt to use Noty toasts (as in Admin: genres (advanced) -> Edit a genre) OR you can try to use Bootstrap to add the errors on the form input fields of the modal window
- After a successful submission, the modal disappears. The master page is NOT reloaded! Instead, you use some JavaScript/jQuery to update the table row of the updated user (and make the table in sync with the database) on your current page. Also a Noty success toast is shown.
TIP
To update a specific user row in the table, you can add the necessary data (id, name, email, ...) to the array returned by the response()->json()
method in the controller method update()
OR get the data from (the input fields of) the bootstrap modal
Delete a user
- Ask for confirmation using a Noty confirm
- After deletion of the user, the master page is NOT reloaded! Instead, you use some JavaScript/jQuery to delete the table row of the deleted user (and make the table in sync with the database) on your current page. Also a Noty success toast is shown.
Example video of advanced user CRUD
- As the master page (search function and pagination) is similar to the basic user CRUD, the video below focuses on the adjusted way of editing/updating and deleting a user
Expert version
Background information
- Both in the basic and advanced user CRUD, the master page is not loaded with jQuery/AJAX (because otherwise we will run into problems with the pagination and the search form)
- Our expert user CRUD is fully AJAX based, and makes use of
- DataTables (https://datatables.net/), a jQuery plugin to implement advanced client-side tables (with pagination, search functionality, ...)
- CDN for Bootstrap: https://cdn.datatables.net/ (click on 'Bootstrap 4' button)
- Info on server-side processing: https://datatables.net/manual/server-side
- Laravel Datatables (https://github.com/yajra/laravel-datatables), a composer package created to handle the server-side processing (via AJAX) for the DataTables plugin
- Quick installation: https://github.com/yajra/laravel-datatables#quick-installation
- Bootstrap switches instead of checkboxes. Changing the state of these switches immediately updates the corresponding database column (
active
oradmin
) of the user via AJAX
- DataTables (https://datatables.net/), a jQuery plugin to implement advanced client-side tables (with pagination, search functionality, ...)