Laravel 12 Admin Panel with Roles & Permissions: In this tutorial, we will build a complete admin panel in Laravel 12 with role-based access control. This is one of the most important features in modern web applications where different users have different permissions.
We will use the popular Spatie Laravel Permission package to manage roles and permissions efficiently. This setup is widely used in real-world applications like admin dashboards, SaaS platforms, and enterprise systems.
📌 What You Will Learn
- Create roles (Admin, Editor, User)
- Assign permissions
- Restrict access using middleware
- Build a secure admin panel
Step 1. Install Laravel 12
composer create-project laravel/laravel laravel-admin cd laravel-admin
Step 2. Setup Database
DB_DATABASE=laravel_admin DB_USERNAME=root DB_PASSWORD=
php artisan migrate
Step 3. Install Spatie Permission Package
composer require spatie/laravel-permission
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" php artisan migrate
Step 4. Setup User Model
Add this trait in User.php:
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
}
Step 5. Create Roles & Permissions
You can create roles using Tinker:
php artisan tinker use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; Role::create(['name' => 'Admin']); Role::create(['name' => 'Editor']); Role::create(['name' => 'User']); Permission::create(['name' => 'create post']); Permission::create(['name' => 'edit post']); Permission::create(['name' => 'delete post']);
Step 6. Assign Permissions to Roles
$admin = Role::findByName('Admin');
$admin->givePermissionTo(['create post', 'edit post', 'delete post']);
$editor = Role::findByName('Editor');
$editor->givePermissionTo(['create post', 'edit post']);
Step 7. Assign Role to User
$user = \App\Models\User::find(1);
$user->assignRole('Admin');
Step 8. Protect Routes (Middleware)
Route::group(['middleware' => ['role:Admin']], function () {
Route::get('/admin', function () {
return "Admin Dashboard";
});
});
Step 9. Blade Example
@role('Admin')
<a href="/admin">Admin Panel</a>
@endrole
@can('edit post')
<button>Edit</button>
@endcan
Step 10. Real Example (Post Management)
Controller example:
public function destroy($id)
{
$this->authorize('delete post');
Post::destroy($id);
return back();
}
✅ Best Practices
- Always use middleware for security
- Keep roles simple and scalable
- Use policies for advanced control
- Cache permissions for performance
🚀 Conclusion
You have successfully built a Laravel 12 admin panel with roles and permissions using Spatie. This system allows you to control user access securely and is widely used in production applications.
In the next tutorial, you can extend this by adding dynamic admin dashboards or multi-auth systems.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How to Use Google Translate Multi Language In Laravel
- Select2 ajax example
- How To Convert HTML To Image/PDF Using Snappy In Laravel 9
- Laravel 56 Login and Logout
- Laravel Email Send
- Default index.php Root Path Change And Remove Public From The Url
- UPI Payment Gateway Integration in PHP/Laravel
- REST API Authentication With Laravel Passport - Webappfix
- Know All About Laravel Mixin Use In Vue Js With Example
- Single Session User Login in Laravel