Laravel Project How to Blocked/Banned Users
Laravel 05-Oct-2020

Laravel Project How to Blocked/Banned Users

Laravel is most popular php framework in the world. Laravel is very simple and easy php framework with many useful functions libraries ,packages etc. In this laravel blocked/banned/suspended users example. We would love to share with you how to banned/blocked/suspended users.

Laravel has provided many features for creating web application with fully featured. Laravel doesn’t include suspending/blocking users for some time. We will use middleware and blocked/suspended/banned users. Sometime we want to blocked/banned/suspended to some users in laravel based project. In this example we will learn how to banned/ blocked/suspended users for few days.

In this laravel users suspend/block example, we will add a one field name blocked_date in users table . We will check users suspend/block or not using the blocked_date field. Good pratice for that always maintain blocked_date in database not use true/false field.

Contents

  • First Install New Laravel Setup
  • Configure .env file
  • Add fillable Propery In Model
  • Auth Scaffolding
  • Middleware Checkblocked
  • Register Middleware
  • Conclusion

First Install New Laravel Setup

In this step, we will download a new laravel 5.7 setup. Use the below command and download it.

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

Configure .env file

Next Go to your project root directory, find .env file and setup database credential here :

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Add fillable propery

Go to app/User.php file and add this fillable property.

protected $fillable = [
     'name', 'email', 'password', 'blocked_date'
 ];
 protected $dates = [
     'blocked_date'
 ];

After successfully add fillable propery in User.php model. Next go to app/datatabase/migrations and open users migration file and put the below code here :

public function up()
 {
     Schema::create('users', function (Blueprint $table) {
         $table->increments('id');
         $table->string('name');
         $table->string('email')->unique()->nullable();
         $table->timestamp('email_verified_at')->nullable();
         $table->string('password')->nullable();
         $table->timestamp('blocked_date')->nullable();
         $table->rememberToken()->nullable();
         $table->timestamps();
     });
 }

Go to app/providers/AppServiceProvider.php and put the below code :

...
use Illuminate\Support\Facades\Schema;
 
....
function boot()
{
    Schema::defaultStringLength(191);
}
... 

Next migrate the table using the below command :

php artisan migrate

Auth Scaffolding

Next we need to use default migration command for creating login, register, and home blade view file with defualt controllers and routes.

php artisan make:auth

Middleware CheckBlocked

Now we will create a middleware for check banned/suspended/blocked users. If user will try to login. We will check them user is blocked or not after that we will go to next request.

First we need to create middleware. use the below command and create middleware.

php artisan make:middleware CheckBlocked

Now we will implement logic of banned/blocked/suspended users. In that time, we will log them out and redirect to login screen with message . Next open the app/http/middleware/CheckBlocked middleware and replace the below function.

<?php
 
namespace App\Http\Middleware;
 
use Closure;
 
class CheckBlocked
{
public function handle($request, Closure $next) {  
 
   if (auth()->check()) 
   {
   if (date('Y-m-d H:i:s') < auth()->user()->blocked_date) {  
      $blocked_days = now()->diffInDays(auth()->user()->blocked_date); 
      $message = 'Your account has been blocked. It will be unblocked after '.$blocked_days.' '.str_plural('day', $blocked_days);        
      auth()->logout();     
      return redirect()->route('login')->withMessage($message);      
     }            
    }      
    return $next($request);   
  } 
}

Register Middleware

Go to app/Http/kerna.php file and register your middleware here, like below.

protected $middlewareGroups = [
 'web' => [
     \App\Http\Middleware\EncryptCookies::class,

     \App\Http\Middleware\CheckBlocked::class,
 ],
Add Error Message

Now we need to add error message in login.blade.php. let’s go to app/resources/views/auth/login.blade.php and to error message on above of login form body.

@if (session('message'))     
   {{ session('message') }}
  @endif 
 
<form method="post" action="{{ route('login') }}">

Conclusion

In this laravel blocked/suspended/bannned users example, we have successfully created a middleware and help of middleware we will check the user is banned/blocked/suspended or not. After that time we will log them.