stylers/laravel-ban
Laravel Ban
Requirements
- PHP >= 8.1
- Laravel ~10.x
Installation
composer require stylers/laravel-ban
You can publish the migration
php artisan vendor:publish --provider="Stylers\LaravelBan\Providers\BanServiceProvider"
After the migration has been published, you can run the migrations
php artisan migrate
Usage
- How to add User to Bannable
use Stylers\LaravelBan\Contracts\Models\Traits\BannableInterface;
use Stylers\LaravelBan\Models\Traits\Bannable;
class User extends Authenticatable implements BannableInterface
{
use Notifiable;
use Bannable;
}
Ban
use Carbon\Carbon;
$user = User::first();
$comment = "Reason of ban."; // ?string
$startAt = Carbon::addWeek(); // ?DateTimeInterface
$endAt = Carbon::now()->addWeeks(2); // ?DateTimeInterface
$ban = $user->ban(); // Ban without comment and timestamps (start_at, end_at) - never expire
$ban = $user->ban($comment, null, $endAt); // Ban for 2 weeks with comment
$ban = $user->ban($comment); // Ban without expire
$ban = $user->ban($comment, $startAt, $endAt); // Ban for a week with comment from next week
Unban
Remove active bans
$user = User::first();
$unbans = $user->unban();
Events
use Stylers\LaravelBan\Events\Banned;
use Stylers\LaravelBan\Events\Unbanned;
Middleware for User
- Update
$routeMiddlewareinApp\Http\Kernel.php
use Stylers\LaravelBan\Http\Middleware\CheckUserBan;
protected $routeMiddleware = [
...
'check_user_ban' => CheckUserBan::class,
];
- Update your routes in
routes/orApp\Providers\RouteServiceProvider
protected function mapWebRoutes()
{
Route::middleware('web', 'check_user_ban')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Related Packages
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 6.0 | >=8.1 | ~10 | MIT | |
| 5.0 | >=8.0 | ~9 | MIT | |
| 4.0 | >=7.3 | ~8 | MIT | |
| 3.0 | >=7.3 | ~7 | MIT | |
| 2.1.0 | >=7.3 | ~6 | MIT | |
| 2.0 | >=7.3 | ~6 | MIT | |
| 1.1.0 | >=7.1.3 | ~5 | MIT | |
| 1.0.1 | >=7.1.3 | ~5 | MIT | |
| 1.0.0 | >=7.1.3 | ~5 | MIT |