Ban Eloquent Models With the Laravel Ban Package
Published on by Paul Redmond
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:
$user->ban(); $user->ban([ 'comment' => 'Enjoy your ban!',]); // A ban that has a ban expiry date$user->ban([ 'expired_at' => '+1 month',]); // Remove a ban$user->unban(); // Boolean checks$user->isBanned();$user->isNotBanned();$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.