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

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

Advanced State Management in React Using Redux Toolkit

Let’s assume we have a new feature for managing products. Instead of preloading its reducer, inject it dynamically when the feature is accessed.

In src/features/products/productsSlice.js:

Dec 09, 2024
Read More
Tutorial
php

Debugging Common Middleware Issues in Laravel

Ensure the middleware is registered in app/Http/Kernel.php under $middleware or $routeMiddleware:

   protected $routeMiddleware = [
       'auth' => \App\Http\Middleware\Authenticate::class,
       'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
   ];

Nov 16, 2024
Read More
Tutorial
php

Laravel Best Practices for Sharing Data Between Views and Controllers

   namespace App\Http\Controllers;

   class ExampleController extends Controller
   {
       public function index()
       {
           $userPreferences = [
               'notifications' => true,
               'language' => 'en',
           ];

           return view('example.index', compact('userPreferences'));
       }
   }
   @if ($userPreferences['notifications'])
       <p>Notifications are enabled.</p>
   @else
       <p>Notifications are disabled.</p>
   @endif

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 More
Code
php bash

Laravel Artisan Commands Cheatsheet

  • Rollback the Last Migration Operation
  php artisan migrate:rollback

Aug 03, 2024
Read More
Code
bash

Generate Model, Controller, and Middleware in Laravel

# Generate a model named 'MyModel'
php artisan make:model MyModel

# Generate a controller named 'MyController'
php artisan make:controller MyController

# Generate a middleware named 'MyMiddleware'
php artisan make:middleware MyMiddleware

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!