{"id":19540949,"url":"https://github.com/qirolab/laravel-bannable","last_synced_at":"2025-04-26T16:32:09.111Z","repository":{"id":32718125,"uuid":"140012721","full_name":"qirolab/laravel-bannable","owner":"qirolab","description":"Laravel bannable package for blocking and banning Eloquent models.","archived":false,"fork":false,"pushed_at":"2024-03-07T16:43:50.000Z","size":77,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-11T11:32:03.640Z","etag":null,"topics":["ban","block","eloquent","forbid","laravel","restrict","user"],"latest_commit_sha":null,"homepage":"https://qirolab.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qirolab.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"custom":"https://www.buymeacoffee.com/qirolab"}},"created_at":"2018-07-06T17:23:13.000Z","updated_at":"2024-05-13T17:42:27.000Z","dependencies_parsed_at":"2024-03-07T17:50:12.367Z","dependency_job_id":null,"html_url":"https://github.com/qirolab/laravel-bannable","commit_stats":{"total_commits":58,"total_committers":3,"mean_commits":"19.333333333333332","dds":"0.15517241379310343","last_synced_commit":"ef30c3b2cf9ad66cb98b50d6def8912198f2cb10"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qirolab%2Flaravel-bannable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qirolab%2Flaravel-bannable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qirolab%2Flaravel-bannable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qirolab%2Flaravel-bannable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qirolab","download_url":"https://codeload.github.com/qirolab/laravel-bannable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224040517,"owners_count":17245758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ban","block","eloquent","forbid","laravel","restrict","user"],"created_at":"2024-11-11T03:08:05.009Z","updated_at":"2024-11-11T03:08:06.335Z","avatar_url":"https://github.com/qirolab.png","language":"PHP","funding_links":["https://www.buymeacoffee.com/qirolab"],"categories":[],"sub_categories":[],"readme":"# Laravel Bannable\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/qirolab/laravel-bannable.svg?style=flat-square)](https://packagist.org/packages/qirolab/laravel-bannable)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/qirolab/laravel-bannable/Tests?label=Tests)](https://github.com/qirolab/laravel-bannable/actions?query=workflow%3ATests+branch%3Amaster)\n[![Styling](https://github.com/qirolab/laravel-bannable/workflows/Check%20\u0026%20fix%20styling/badge.svg)](https://github.com/qirolab/laravel-bannable/actions?query=workflow%3A%22Check+%26+fix+styling%22)\n[![Psalm](https://github.com/qirolab/laravel-bannable/workflows/Psalm/badge.svg)](https://github.com/qirolab/laravel-bannable/actions?query=workflow%3APsalm)\n[![GitHub](https://img.shields.io/github/license/qirolab/laravel-bannable.svg)](https://github.com/qirolab/laravel-bannable)\n\nLaravel bannable package for blocking and banning Eloquent models. Using this package any model can be made bannable such as Organizations, Teams, Groups, and others.\n\n## Installation\n\nDownload package into the project using Composer.\n\n```bash\ncomposer require qirolab/laravel-bannable\n```\n\n### Registering package\n\u003e Laravel 5.5 (or higher) uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.\n\nFor Laravel 5.4 or earlier releases version include the service provider within `app/config/app.php`:\n\n```php\n'providers' =\u003e [\n    Qirolab\\Laravel\\Bannable\\BannableServiceProvider::class,\n],\n```\n\n### Prepare Migration\nNow need to add nullable `banned_at` timestamp column to model. So, create a new migration file.\n\n```bash\nphp artisan make:migration add_banned_at_column_to_users_table\n```\n\nAdd `$table-\u003etimestamp('banned_at')-\u003enullable();` in this new migration file as in below example.\n\n```php\n\u003c?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass AddBannedAtColumnToUsersTable extends Migration\n{\n    public function up()\n    {\n        Schema::table('users', function (Blueprint $table) {\n            $table-\u003etimestamp('banned_at')-\u003enullable();\n        });\n    }\n\n    public function down()\n    {\n        Schema::table('users', function (Blueprint $table) {\n            $table-\u003edropColumn('banned_at');\n        });\n    }\n}\n```\n\nNow run migration.\n```bash\nphp artisan migrate\n```\n\n### Prepare bannable model\nUse `Bannable` trait in the Model as in below example.\n\n```\nuse Qirolab\\Laravel\\Bannable\\Traits\\Bannable;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\n\nclass User extends Authenticatable\n{\n    use Bannable;\n}\n```\n\n## Available methods\n\n### Ban model entity.\n```php\n$user-\u003eban();\n```\n\n### Ban model entity with reason comment\n```php\n$user-\u003eban([\n    'comment' =\u003e 'ban comment!',\n]);\n```\n\n### Ban model entity with expire time\n\nHere `expired_at` attribute could be `\\Carbon\\Carbon` instance or any time string which could be parsed by \\Carbon\\Carbon::parse($string) method:\n```php\n$user-\u003eban([\n    'expired_at' =\u003e '2086-03-28 00:00:00',\n]);\n```\nor\n\n```php\n$user-\u003eban([\n    'expired_at' =\u003e '+1 year',\n]);\n```\n\nor\n\n```php\n$date = Carbon::now()-\u003eaddWeeks(2);\n\n$user-\u003eban([\n    'expired_at' =\u003e $date,\n]);\n```\n\n### Remove ban model\nOn `unban` all related ban models are soft deletes.\n\n```php\n$user-\u003eunban();\n```\n\n### Check if entity is banned\n```php\n$user-\u003eisBanned();\n```\n\n### Check if entity is not banned\n```php\n$user-\u003eisNotBanned();\n```\n\n### Delete expired bans manually\n```php\nQirolab\\Laravel\\Bannable\\Models\\Ban::deleteExpired();\n```\n\n## Scopes\n\n### Get all models which are not banned\n```php\n$users = User::withoutBanned()-\u003eget();\n```\n\n### Get banned and not banned models\n```php\n$users = User::withBanned()-\u003eget();\n```\n\n### Get only banned models\n```php\n$users = User::onlyBanned()-\u003eget();\n```\n\n### Disable scope that hides banned models entity by default\n\nBy default all banned models will be hidden. To disable query scopes all the time you can define `disableBannedAtScope` method in bannable model.\n\n```php\n/**\n * Determine which BannedAtScope should be applied or not.\n *\n * @return bool\n */\npublic function disableBannedAtScope()\n{\n    return true;\n}\n```\n\n## Events\n\nOn model entity ban `\\Qirolab\\Laravel\\Bannable\\Events\\ModelWasBanned` event is fired.\n\nOn model entity unban `\\Qirolab\\Laravel\\Bannable\\Events\\ModelWasUnbanned` event is fired.\n\n## Middleware\nTo prevent banned users to go to protected routes `Qirolab\\Laravel\\Bannable\\Middleware\\ForbidBannedUser` middleware is created.\n\nRegister it in $routeMiddleware array of app/Http/Kernel.php file:\n\nprotected $routeMiddleware = [\n    'isBannedUser' =\u003e \\Qirolab\\Laravel\\Bannable\\Middleware\\ForbidBannedUser::class,\n]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqirolab%2Flaravel-bannable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqirolab%2Flaravel-bannable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqirolab%2Flaravel-bannable/lists"}