Contact US

Get Your Project Done by our experts

How to Ban | Block | Disable and unBan user for login in laravel

How to Ban or block user for login in Laravel

So guys to BAN or BLOCK user for login, we have already setup with login and registration in laravel using Auth Command

Step 1: By the following below command:

php artisan make:auth

Step 2:  All the setup for login and register is successfully done. Now, let's edit users table which is available in following folder: database / migrations / yoursomedate_create_users_table.php and add the type column for it:

    $table->tinyInteger('isban')->default('0'); //Add in UserTable before timestamps

   Note: the value which you store in isban column: 0 = un_banned / 1 = isbanned_now.  

Step 3: migrate this table OR go to your phpmyadmin and add one column named isban before created_at column in Users Table  into database.

Step 4: Go to your Model, where path is App/User.php  and then paste the column name isban to your protected $fillable as given below:

  protected $fillable = [

Step 5: Now, create a middleware for checking the user is banned or not banned with the name UserMiddleware:

    php artisan make:middleware UserMiddleware

after middleware generated go to your UserMiddleware as follows: app/Http/Middleware/UserMiddleware.php and paste the below code:

use Illuminate\Support\Facades\Auth;

public function handle($requestClosure $next)
        if(Auth::check() && Auth::user()->isban)
            $banned = Auth::user()->isban == "1"; // "1"= user is banned / "0"= user is unBanned

            if ($banned == 1) {
                $message = 'Your account has been Banned. Please contact administrator.';
            return redirect()->route('login')
                ->withErrors(['email' => 'Your account has been Banned. Please contact administrator.'])

        return $next($request);

Step 6: Now Register your Middleware just you created named UserMiddleware with the following path: app/Http/Kernel.php and goto the routeMiddleware and paste the below code:

protected $routeMiddleware = [

        'isUser' => \App\Http\Middleware\UserMiddleware::class, //Add to the end

Step 7: Set your Redirection when you are logging as user or admin or any other role provided. Goto path - app/Http/Controller/Auth/LoginController.php and paste the below code:

use Illuminate\Support\Facades\Auth;

    //  Use this below to redirect 
    protected $redirectTo = '/home';

    // OR-Else => use if you already used to redirecting authentication as per role.
    protected function authenticated()
        if(Auth::user()->role_as == '0'// Normal or Default User Login
            return redirect('/home');

Step 8: Lets Setup the Route for this Process/Checking of isbanned and unbanned user  as give below:

    Route::group(['middleware' => ['auth','isUser']], function () {



Step 9: Finally, Now you have to paste the code the check the message you get from your UserMiddleware created from your path resource/view/auth/login.blade.php page file.

    @if (session('status'))
        <div class="alert alert-danger">{{ session('status'}}</div>

    <form method="POST" action="{{ route('login') }}">

Step 10: Finally we are done with above thing to ban a user, so now lets register the 1 user. Example: Email:, Password: 12345678, and now click to register, so once you get registered your isban column stores the default value as "0", so if you want to block the user, change the isban column value to "1".  by below command or directly on phpmyadmin or create a CRUD:

    $ php artisan tinker

    >>> use App\User;
    >>>User::where('email', '')->update(['isban' => '1']);

Step 11: Serve your artisan and get started with it. Thank you.

    php artisan serve