Install the Package
composer require cybercog/laravel-nova-ban
Publish the migration (if needed) and run it:
php artisan vendor:publish --provider="CyberCog\NovaBan\NovaBanServiceProvider" --tag="migrations"
php artisan migrate
Prepare a Bannable Model
Add the CyberCog\Ban\Traits\Bannable trait to your Eloquent model (e.g., User):
use CyberCog\Ban\Traits\Bannable;
class User extends Authenticatable
{
use Bannable;
// ...
}
Register Ban Actions in Nova Resource Extend your Nova resource to include ban actions:
use CyberCog\NovaBan\Actions\Ban;
use CyberCog\NovaBan\Actions\Unban;
class UserResource extends Resource
{
public static $actions = [
Ban::make(),
Unban::make(),
];
// ...
}
First Use Case Visit your Nova dashboard, locate a user, and click the "Ban" action to test the functionality.
Banning Users
Ban::make()->withReasonField('ban_reason');
$user->ban('Spam behavior', $adminId);
Unbanning Users
$user->unban($adminId);
Integration with Nova Tools
use CyberCog\NovaBan\Filters\Banned;
class UserResource extends Resource
{
public static $filters = [
new Banned,
];
}
use CyberCog\NovaBan\Fields\BanStatus;
class UserResource extends Resource
{
public function fields(Request $request)
{
return [
// ...
BanStatus::make(),
];
}
}
Event Listeners
use CyberCog\Ban\Events\UserBanned;
use CyberCog\Ban\Events\UserUnbanned;
event(new UserBanned($user, $admin, 'Reason'));
Custom Ban Logic
Override the isBannable() method in your model to enforce rules:
public function isBannable(): bool
{
return $this->isActive() && !$this->isAdmin();
}
Ban Expiry
Set temporary bans by extending the Ban action:
Ban::make()->temporary(30); // 30-day ban
Multi-Model Support
Reuse the trait across multiple models (e.g., User, Vendor) with shared ban logic.
Migration Conflicts
bans table already exists, the package’s migration may fail. Manually adjust the schema or drop the table first.bans table in database/migrations/ before publishing.Permission Issues
ban and unban actions are guarded in your Nova policy:
public function authorizeBanAction(User $user, $model)
{
return $user->isAdmin();
}
Soft Deletes vs. Bans
SoftDeletes, ensure the deleted_at column doesn’t interfere with ban logic.Caching Quirks
nova:cache-clear after testing.Log Ban Events Add logging to debug ban/unban triggers:
\CyberCog\Ban\Events\UserBanned::dispatch($user, $admin, 'Reason');
Check Laravel logs (storage/logs/laravel.log) for entries.
Verify Database
Inspect the bans table directly to confirm ban records:
SELECT * FROM bans WHERE bannable_id = [user_id];
Nova Toolbar Errors If actions fail silently, enable Nova’s debug mode:
NOVA_DEBUG=true
Custom Ban Fields Extend the ban modal by publishing and overriding views:
php artisan vendor:publish --provider="CyberCog\NovaBan\NovaBanServiceProvider" --tag="views"
Modify resources/views/vendor/nova-ban/....
API Integration Expose ban logic via API routes:
Route::post('/users/{user}/ban', function (User $user) {
$user->ban(request('reason'), auth()->id());
return response()->json(['status' => 'banned']);
});
Localization Translate ban messages by publishing language files:
php artisan vendor:publish --provider="CyberCog\NovaBan\NovaBanServiceProvider" --tag="lang"
Update resources/lang/en/nova-ban.php.
Testing Use the package’s test helpers to assert ban status:
$this->assertTrue($user->isBanned());
$this->assertEquals('Reason', $user->ban->reason);
How can I help you explore Laravel packages today?