What are we going to cover this round?
This episode mainly focuses on the “Admin-Facing” functionality. If you missed the last episode, you need to read through and complete your Backpack setup.
Laravel Backpack provides extensive FREE useful packages for you to build your web application fast! When you finish this tutorial, these functionalities will be added. Have fun!
✴️ Admin Roles & Permission
✴️ Users Management
✴️ Pages Management
✴️ Post Publication
🌐 Admin Management
We’re going to install the Permission Manager to manage the Admin’s Roles & Permissions.
1. Install via Composer
sail composer require backpack/permissionmanager
2. Publish the necessary migrations of spatie/laravel-permission
sail artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"
3. Run the migration
sail artisan migrate
4. Publish the config of Spatie Permissions packages
sail artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
5. Publish the config for the backpack/permission manager
sail artisan vendor:publish --provider="Backpack\PermissionManager\PermissionManagerServiceProvider" --tag="config"
We need to modify the Model and Multi-Guards options…
6. Add traits in the Admin
Model
CrudTrait
and HasRoles
traits (line 14) are required for permission manager.
7. Copy CRUD controller
The Permission Manager comes with the CRUD Controller, but it is using \user
as the route, so we need to copy and modify it.
Copy the file:
cp vendor/backpack/permissionmanager/src/app/Http/Controllers/UserCrudController.php app/Http/Controllers/Admin/AdminCrudController.php
Alternatively, you can create the file app/Http/Controllers/Admin/AdminCrudController.php
with the content below:
It is lengthy, but I just changed the namespace
(line 3), class
(line 10) name and the setRoute()
(line 21).
8. Register routes
Then we need to bind the route with the CRUD controller above.
Add the line in /routes/backpack/custom.php
Route::crud('admin', 'AdminCrudController');
The final result:
9. Add menu items
Finally, add the menu links on the sidebar menu.
10. Update the terms
You should notice it use “Users” in the Permission Manager, but you can change it easily by modifying the language file.
Create the file resources/lang/vendor/backpack/en/permissionmanager.php
with the content below:
The result will be like this:
We will not cover how to utilize the Role & Permission for now. We will come back in the future episode for controlling which roles/permissions can access which part of the Admin Panel.
🌐 Users Management
Since we’re able to manage Admins, then we can also build a user management using the core functionality of Laravel Backpack.
💡 Reference: Crash Course from the Laravel Backpack Official Documentation
In this round, we will only briefly touch the CRUD by referencing existing Admin CRUD.
The migration, model of the User
model was created previously. Run this command to generate other components:
sail artisan backpack:crud user
You should see three files is modified (yellow icon), and two files added (green icon)
- ✏️ Model —
User.php
CrudTrait
is added, make it become a CRUD-capable entity. - ✏️ Menu —
sidebar_content.blade.php
The “Users” menu item is added, you can change the icon (la-question
). - ✏️ Route —
custom.php
The CRUD resource routes are added for all related requests, but we need to override the route in the Permission Manager. So you need to remove the line below in thecustom.php
:
Route::crud('user', 'UserCrudController'); <<--- Remove this
- ✏️ Route —
web.php
Just copy the lines from line 26, we put it here (web.php
) due to the order of route files loading. This will override the Permission Manager route(s) for/admin/user
. This should be the easiest way for beginners.
- ➕ Store Request Validation —
UserStoreCrudRequest.php
We need to separate theUserRequest.php
due to different validation rules for store/update. So, you need to deleteUserRequest.php
and createUserStoreCrudRequest.php
.
- ➕ Update Request Validation —
UserUpdateCrudRequest.php
We need to separate theUserRequest.php
due to different validation rules for store/update. So, you need to deleteUserRequest.php
and createUserUpdateCrudRequest.php
. For “update”, the email is unique except for the original email address of the user. You can read the Laravel Office Docs for more details.
- ➕ Controller—
UserCrudController.php
Now, the route/admin/user
is pointing toUserCrudController.php
. I refer to the fileapp/Http/Controllers/Admin/AdminCrudController.php
and here is the end result you can take away…
Finally, these are the files modified/added:
🎉 Done! You can test the User CRUD in your Admin panel now!
🌐 Page Management
You will soon own a homepage, but you definitely do not want to open your VS Code to edit your page content for updating the content. This is what CMS (Content Management System) comes into the scene, a well-known example you might hear before — WordPress.
1. Create Sample Page Template (app/PageTemplates.php
)
This is the sample page template, we will add more pages in the future.
2. Install via Composer
sail composer require backpack/pagemanager
3. Publish vendor files
sail artisan vendor:publish --provider="Backpack\PageManager\PageManagerServiceProvider"
4. Run migrations
sail artisan migrate
5. Add Sidebar Menu item (resources/views/vendor/backpack/base/inc/sidebar_content.blade.php
)
Paste the “Page” menu item with the menu order you want.
<li class='nav-item'><a class='nav-link' href='{{ backpack_url('page') }}'><i class='nav-icon la la-file-o'></i> <span>Pages</span></a></li>
That’s it! We are not going to cover the frontend portion for now.
🌐 Post Publication
You can now manage your website pages, so you would like to have a blog post section to connect your audiences.
1. Install via Composer
sail composer require backpack/newscrud
2. Publish vendor files
sail artisan vendor:publish --provider="Backpack\NewsCRUD\NewsCRUDServiceProvider"
3. Run migrations
sail artisan migrate
4. Add Sidebar Menu item
Same as what all CRUDs you have done, you need to add the menu items to your sidebar (line 4 to line 11).
This is the final sidebar.blade.php
for this episode.
Thanks for following this lengthy tutorial, next episode will focus on the “Client-Facing” section (i.e. Your Homepage). Please follow me to get the latest updates! Thank you.
If you like my article, please clap, follow and share my article (or you could ☕ buy me a coffee ☕ ). Take care and see you soon!