DeveloperBreeze

Middleware to Restrict Access to Admins in Laravel

php
// 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');
}

Related Posts

More content you might like

Tutorial
javascript

Advanced State Management in React Using Redux Toolkit

Dynamic injection of reducers covered earlier is a key advanced pattern for large applications.

Use libraries like normalizr to normalize deeply nested API responses for better state handling:

Dec 09, 2024
Read More
Tutorial
php

Debugging Common Middleware Issues in Laravel

If middleware isn’t being applied:

  • Confirm it’s assigned to the route.
  • Ensure there are no typos in the middleware alias or class name.

Nov 16, 2024
Read More
Tutorial
php

Laravel Best Practices for Sharing Data Between Views and Controllers

   @if ($userPreferences['notifications'])
       <p>Notifications are enabled.</p>
   @else
       <p>Notifications are disabled.</p>
   @endif

For data that depends on the request or user context, use middleware.

Nov 16, 2024
Read More
Article
javascript

20 Useful Node.js tips to improve your Node.js development skills:

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 More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!