// Middleware to check if the user is an admin
public function handle($request, Closure $next)
{
// Verify the user is authenticated and an admin
if (auth()->check() && auth()->user()->isAdmin) {
return $next($request);
}
// Redirect non-admin users to the home page
return redirect()->route('home');
}Middleware to Restrict Access to Admins in Laravel
php
Related Posts
More content you might like
Tutorial
javascript
Advanced State Management in React Using Redux Toolkit
npx create-react-app redux-toolkit-advanced
cd redux-toolkit-advanced
npm install @reduxjs/toolkit react-redux axios reselectOrganize the folders for scalability:
Dec 09, 2024
Read More Tutorial
php
Debugging Common Middleware Issues in Laravel
Route::middleware(['auth'])->group(function () {
Route::middleware(['admin'])->group(function () {
Route::get('/admin', [AdminController::class, 'index']);
});
});Example: Middleware requires session data, but StartSession middleware hasn’t run yet.
Nov 16, 2024
Read More Tutorial
php
Laravel Best Practices for Sharing Data Between Views and Controllers
Add the middleware to the web middleware group in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// Other middleware
\App\Http\Middleware\ShareUserPreferences::class,
],
];Nov 16, 2024
Read More Article
javascript
20 Useful Node.js tips to improve your Node.js development skills:
These Node.js tips will help you write more robust, secure, and efficient Node.js applications and improve your development workflow.
Oct 24, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!