Kritim Yantra
Mar 26, 2025
In this comprehensive guide, we’ll create a Laravel 12 admin panel with role-based access control (RBAC) using the popular Spatie Laravel-Permission package. This setup allows you to:
✅ Create roles (Admin, Editor, User, etc.)
✅ Assign permissions (create, edit, delete, view)
✅ Restrict access based on user roles
✅ Manage users and permissions via an admin dashboard
Before starting, ensure you have:
composer create-project laravel/laravel laravel-admin-panel
cd laravel-admin-panel
composer require laravel/breeze --dev
php artisan breeze:install
php artisan migrate
npm install && npm run dev
Modify database/seeders/DatabaseSeeder.php
:
use App\Models\User;
public function run()
{
$admin = User::create([
'name' => 'Admin',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
]);
}
Run the seeder:
php artisan db:seed
composer require spatie/laravel-permission
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
Update bootstrap/app.php
:
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
]);
})
Modify DatabaseSeeder.php
:
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
public function run()
{
// Create roles
$adminRole = Role::create(['name' => 'admin']);
$editorRole = Role::create(['name' => 'editor']);
$userRole = Role::create(['name' => 'user']);
// Create permissions
$permissions = [
'create-post',
'edit-post',
'delete-post',
'view-post',
];
foreach ($permissions as $permission) {
Permission::create(['name' => $permission]);
}
// Assign all permissions to admin
$adminRole->syncPermissions($permissions);
// Assign limited permissions to editor
$editorRole->syncPermissions(['create-post', 'edit-post', 'view-post']);
// Assign default user role
$user = User::create([
'name' => 'Admin',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
]);
$user->assignRole('admin');
}
Run the seeder:
php artisan db:seed
php artisan make:controller AdminController --resource
Update routes/web.php
:
use App\Http\Controllers\AdminController;
Route::middleware(['auth', 'role:admin'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
Route::resource('/admin/users', AdminController::class);
});
Create resources/views/admin/dashboard.blade.php
:
@extends('layouts.app')
@section('content')
<div class="container">
<h1>Admin Dashboard</h1>
<div class="card">
<div class="card-header">
<h3>Manage Users</h3>
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Roles</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
@foreach($user->roles as $role)
<span class="badge bg-primary">{{ $role->name }}</span>
@endforeach
</td>
<td>
<a href="{{ route('admin.users.edit', $user->id) }}" class="btn btn-sm btn-warning">Edit</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection
Update AdminController.php
:
public function __construct()
{
$this->middleware(['auth', 'role:admin']);
}
public function dashboard()
{
$users = User::with('roles')->get();
return view('admin.dashboard', compact('users'));
}
@can('edit-post')
<button class="btn btn-sm btn-warning">Edit Post</button>
@endcan
Create resources/views/admin/users/edit.blade.php
:
<form action="{{ route('admin.users.update', $user->id) }}" method="POST">
@csrf
@method('PUT')
<div class="mb-3">
<label>Roles</label>
@foreach($roles as $role)
<div class="form-check">
<input type="checkbox" name="roles[]" value="{{ $role->name }}"
{{ $user->hasRole($role->name) ? 'checked' : '' }}>
<label>{{ $role->name }}</label>
</div>
@endforeach
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
public function update(Request $request, User $user)
{
$user->syncRoles($request->roles);
return redirect()->route('admin.dashboard')->with('success', 'Roles updated!');
}
Route::middleware(['auth', 'role:admin|editor'])->group(function () {
Route::get('/posts/create', [PostController::class, 'create']);
});
if (auth()->user()->can('edit-post')) {
// Allow editing
}
You’ve successfully built a Laravel 12 admin panel with role-based permissions!
✅ User Authentication (Laravel Breeze)
✅ Role & Permission Management (Spatie Package)
✅ Admin Dashboard (User & Role Management)
✅ Middleware Protection (Restricted Access)
Now you can securely manage users, roles, and permissions in your Laravel application! 🚀
Let me know if you need any clarifications. Happy coding! 😊
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 23, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google