Blog Detail


Make Any Eloquent Model Bannable in Minutes with Laravel Ban cover image

arrow_back Make Any Eloquent Model Bannable in Minutes with Laravel Ban

Laravel Ban simplifies management of the Eloquent model’s ban. Make any model bannable in minutes! The use case is not limited to the User model, any Eloquent model could be banned: Organizations, Teams, Groups, and others. It has the following features:

  • Model can have many bans.
  • Removed bans kept in history as soft deleted records.
  • Most parts of the logic is handled by the BanService.
  • Has middleware to prevent banned user route access.
  • Use case is not limited to User model, any Eloquent model could be banned.
  • Events firing on models ban and unban.
  • Designed to work with Laravel Eloquent models.
  • Has Laravel Nova support.
  • Using contracts to keep high customization capabilities.
  • Using traits to get functionality out of the box.


First, pull in the package through Composer:

$ composer require cybercog/laravel-ban

Registering package

The package will automatically register itself. This step is required for Laravel 5.4 or earlier releases only.

Include the service provider within


'providers' => [

Apply database migrations

At last you need to publish and run database migrations:

$ php artisan vendor:publish --provider="Cog\Laravel\Ban\Providers\BanServiceProvider" --tag="migrations"
$ php artisan migrate


Prepare bannable model

use Cog\Contracts\Ban\Bannable as BannableContract;
use Cog\Laravel\Ban\Traits\Bannable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements BannableContract
    use Bannable;

Prepare bannable model database table

The bannable model must have a nullable timestamp column named banned_at. This value used as flag and simplify checks if user was banned. If you are trying to make the default Laravel User model to be bannable you can use the example below.

Create a new migration file

$ php artisan make:migration add_banned_at_column_to_users_table

Then insert the following code into migration file:


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddBannedAtColumnToUsersTable extends Migration
    public function up()
        Schema::table('users', function (Blueprint $table) {
    public function down()
        Schema::table('users', function (Blueprint $table) {

Available methods

Apply ban for the entity


Apply ban for the entity with reason comment

    'comment' => 'Enjoy your ban!',

Apply ban for the entity which will be deleted over time

    'expired_at' => '2086-03-28 00:00:00',

expired_at attribute could be \Carbon\Carbon instance or any string which could be parsed by \Carbon\Carbon::parse($string) method:

    'expired_at' => '+1 month',

Remove ban from entity


On unban all related ban models are soft deletes.

Check if entity is banned


Check if entity is not banned


Delete expired bans manually


Determine if ban is permanent

$ban = $user->ban();
$ban->isPermanent(); // true

Or pass null value.

$ban = $user->ban([
   'expired_at' => null,

$ban->isPermanent(); // true

Determine if ban is temporary

$ban = $user->ban([
   'expired_at' => '2086-03-28 00:00:00',

$ban->isTemporary(); // true

This package has a lot more For more details, you can visit Github.

Published at : 18-05-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project