// 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
import usersReducer, { addUser } from './usersSlice';
test('should handle adding a user', () => {
const initialState = { entities: [] };
const user = { id: 1, name: 'John Doe' };
const nextState = usersReducer(initialState, addUser(user));
expect(nextState.entities).toEqual([user]);
});Dynamic injection of reducers covered earlier is a key advanced pattern for large applications.
Dec 09, 2024
Read More Tutorial
php
Debugging Common Middleware Issues in Laravel
If middleware is missing, ensure you’ve assigned it:
Route::get('/dashboard', [DashboardController::class, 'index'])->middleware(['auth', 'verified']);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:
No preview available for this content.
Oct 24, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!