Ban Eloquent Models With the Laravel Ban Package

Packages

December 2nd, 2021

laravel-ban-featured.png

Laravel Ban is a package that simplifies blocking and banning Eloquent models. Using this package, you can make a model "bannable" in minutes.

The use case for this package goes beyond a User model; you can add the concept of a ban to any Eloquent model. The main features of this package include:

  • 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.

The package has a nice API that makes banning the model simple:

1$user->ban();
2 
3$user->ban([
4 'comment' => 'Enjoy your ban!',
5]);
6 
7// A ban that has a ban expiry date
8$user->ban([
9 'expired_at' => '+1 month',
10]);
11 
12// Remove a ban
13$user->unban();
14 
15// Boolean checks
16$user->isBanned();
17$user->isNotBanned();
18$ban->isTemporary();

The package also provides nice conveniences like middleware, events, scopes, and auto-applying scopes to a model.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners