How can we block any user account in Laravel 8

How can we block any user account in Laravel 8

How can we block any user account in Laravel 8

In this tutorial I am going to show how an user account using laravel middleware.

If an user is banned or blocked it will redirect back to login page with an error message.

Step 1

Install Laravel Version8

First of all install a fresh laravel project, download and install Laravel 8 using the below command

composer create-project --prefer-dist laravel/laravel larablockuser

Step 2

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel project. After creating database ,set database credential in application’s .env file.


Step 3

Now, use the below command to create default laravel tables automatically.

php artisan migrate

Step 4

    class User extends Authenticatable{
        protected $fillable = [
            'name', 'email', 'password', 'blocked_until'  
        protected $dates = [

Step 5

Create Laravel Middleware CheckBlockedUser

php artisan make:middleware CheckBlockedUser

    class CheckBLockedUser{
        public function handle($request, Closure $next)
            if (auth()->check() && auth()->user()->blocked_until && now()->lessThan(auth()->user()->blocked_until)) {
            $blocked_days = now()->diffInDays(auth()->user()->blocked_date); 
            $message = 'Your account has been suspended for '.$blocked_days.' '.str_plural('day', $blocked_days).'. Please contact administrator.';    
            return redirect()->route('login')->withMessage($message); 
            return $next($request);

Step 6

Register Laravel MiddlewStep 5-are

Open app/Http/Kernel.php file and register CheckBlocked middleware here, like below –
    protected $middlewareGroups = [
    'web' => [
        // ... other middleware classes


Step 7

Add Error Message

app/resources/views/auth/login.blade.php file and add error message on above of login form body, like below –
        <div class="card-body">
            @if (session('message'))
                <div class="alert alert-danger">{{ session('message') }}</div>   
            <form method="POST" action="{{ route('login') }}">


php artisan serve

Hit the given url:-


Output will be like this:-

login form
I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.

Our Offices


F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163


13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079


15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147