PHPackages                             cjmellor/blockade - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. cjmellor/blockade

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

cjmellor/blockade
=================

Allow a User Model to block another User Model

v1.2.0(2y ago)2014MITPHPPHP ^8.2|^8.3CI passing

Since Mar 17Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/cjmellor/blockade)[ Packagist](https://packagist.org/packages/cjmellor/blockade)[ Docs](https://github.com/cjmellor/blockade)[ RSS](/packages/cjmellor-blockade/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (7)Versions (8)Used By (0)

Allow a User Model to Block Another User Model
==============================================

[](#allow-a-user-model-to-block-another-user-model)

[![Packagist Version](https://camo.githubusercontent.com/cc289d44a0784f726b2750871ea498e86518cb7a272930126de4e6b54558d028/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636a6d656c6c6f722f626c6f636b6164653f636f6c6f723d7267622532383536253230313839253230323438253239266c6162656c3d72656c65617365267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/cc289d44a0784f726b2750871ea498e86518cb7a272930126de4e6b54558d028/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636a6d656c6c6f722f626c6f636b6164653f636f6c6f723d7267622532383536253230313839253230323438253239266c6162656c3d72656c65617365267374796c653d666f722d7468652d6261646765)[![GitHub Tests Action Status](https://camo.githubusercontent.com/c74d6d2dcbae32e462f33f311b684816d262069f0e5b65d2db019f055c77a26e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636a6d656c6c6f722f626c6f636b6164652f72756e2d706573742e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666f722d7468652d626164676526636f6c6f723d726762253238313334253230323339253230313238253239)](https://github.com/cjmellor/blockade/actions?query=workflow%3Arun-tests+branch%3Amain)[![Packagist Downloads](https://camo.githubusercontent.com/92ad01ce18325ed09a1529805fc7f92a61e0b04a42ccd179d21c5aa8485f6885/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636a6d656c6c6f722f626c6f636b6164653f636f6c6f723d7267622532383234392532303131352532303232253239267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/92ad01ce18325ed09a1529805fc7f92a61e0b04a42ccd179d21c5aa8485f6885/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636a6d656c6c6f722f626c6f636b6164653f636f6c6f723d7267622532383234392532303131352532303232253239267374796c653d666f722d7468652d6261646765)[![Packagist PHP Version](https://camo.githubusercontent.com/289c78355610ecabe29aa537cfc52cde1a488cc5d4b985359c206224b1958281/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f636a6d656c6c6f722f626c6f636b6164652f7068703f636f6c6f723d726762253238313635253230313830253230323532253239266c6f676f3d706870266c6f676f436f6c6f723d726762253238313635253230313830253230323532253239267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/289c78355610ecabe29aa537cfc52cde1a488cc5d4b985359c206224b1958281/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f636a6d656c6c6f722f626c6f636b6164652f7068703f636f6c6f723d726762253238313635253230313830253230323532253239266c6f676f3d706870266c6f676f436f6c6f723d726762253238313635253230313830253230323532253239267374796c653d666f722d7468652d6261646765)[![Laravel Version](https://camo.githubusercontent.com/74ff7fca259d5de1178d0c71d4370c2548ee770a332fd1dbaac4ce7a7d769141/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d25354531302e302d7267622832333525323036382532303530293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)](https://camo.githubusercontent.com/74ff7fca259d5de1178d0c71d4370c2548ee770a332fd1dbaac4ce7a7d769141/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d25354531302e302d7267622832333525323036382532303530293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)

Allow a User to block another User. Blocking a User will prevent the blocker from seeing the blockees data.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require cjmellor/blockade
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="blockade-migrations"

php artisan migrate
```

and publish the config file with:

```
php artisan vendor:publish --tag="blockade-config"
```

This is the contents of the published config file:

```
return [
    /**
     * If your User model different to the default, you can specify it here.
     */
    'user_model' => 'App\Models\User',

    /**
     * Specify the user model's foreign key.
     */
    'user_foreign_key' => 'user_id',

    /**
     * Specify the table name for the blocks table.
     */
    'blocks_table' => 'blocks',

    /**
     * Specify the foreign key for the blocker.
     */
    'blocker_foreign_key' => 'blocker_id',

    /**
     * Specify the foreign key for the blocked.
     */
    'blocked_foreign_key' => 'blocked_id',

    /**
     * Schedule the cleanup of expired blocks.
     */
    'schedule_cleanup' => false,
];
```

Usage
-----

[](#usage)

### Using the Traits

[](#using-the-traits)

#### CanBlock

[](#canblock)

Add the `CanBlock` trait to your User model.

This trait should only ever be added to your User model.

```
use Cjmellor\Blockade\Concerns\CanBlock;

class User
{
    use CanBlock;

    // ...
}
```

#### HasBlocked

[](#hasblocked)

Use this trait in your Models where you would not want a User to see the Model of a User they have blocked.

> Example: you have a `Comment` Model and User 7 has blocked User 8 so User 7 should not be able to see any comments made by User 8.

```
use Cjmellor\Blockade\Concerns\HasBlocked;

class Comment
{
    use HasBlocked;

    // ...
}
```

### Block a User

[](#block-a-user)

A User can block another User by supplying a User model, or just the ID of the User to be blocked.

```
$user->block(User::find(2)));

// or

$user->block(2);
```

> **Note:**You cannot block yourself.

### Unblock a User

[](#unblock-a-user)

A User can unblock another User by supplying a User model, or just the ID of the User to be unblocked.

```
$user->unblock(User::find(2)));

// or

$user->unblock(2);
```

### Checking if a User is blocked

[](#checking-if-a-user-is-blocked)

You can run a check to see if you're already blocking a User or not.

```
$user->isBlocking(User::find(2)));

// or

$user->isBlocking(2);
```

### Set a block expiry

[](#set-a-block-expiry)

You can set an expiry date when blocking a User. This will automatically unblock the User after the expiry date has passed.

```
$user->block(User::find(2), expiresAt: now()->addDays(7)));
```

This example will block the User for 7 days. After 7 days, the User will be unblocked.

#### ⏱️ Scheduling the cleanup of expired blocks

[](#️-scheduling-the-cleanup-of-expired-blocks)

By default, a blocked User with an expiry will not be removed after the expiry time as past.

If you want to automatically remove expired blocks, you can set the `schedule_cleanup` config value in the `config/blockade` config file to `true`.

If you want more control over when the cleanup is run, there is an artisan command that can be run to remove expired blocks.

```
php artisan unblock:expired
```

You can also schedule this command to run automatically.

```
$schedule->command('unblock:expired')->everyMinute();
```

> **Note**
>
> Don't forget to add the command to the `App\Console\Kernel` `schedule()` method.

### Events

[](#events)

When a User is blocked, a `UserBlocked` event is fired.

When a User is unblocked, a `UserUnblocked` event is fired.

The `User` Model of the blocked or unblocked User is passed to the event so you can listen for these events and perform further actions.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance61

Regular maintenance activity

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 56% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~93 days

Total

5

Last Release

776d ago

PHP version history (2 changes)v1.0.0PHP ^8.1

v1.2.0PHP ^8.2|^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/a50ec73c5a205880cbeeab7dc86f12acbe34eace997d01772d9380a1b2455390?d=identicon)[cmellor](/maintainers/cmellor)

---

Top Contributors

[![cjmellor](https://avatars.githubusercontent.com/u/1848476?v=4)](https://github.com/cjmellor "cjmellor (47 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (24 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (13 commits)")

---

Tags

laravelcjmellorblockade

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/cjmellor-blockade/health.svg)

```
[![Health](https://phpackages.com/badges/cjmellor-blockade/health.svg)](https://phpackages.com/packages/cjmellor-blockade)
```

###  Alternatives

[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[socialiteproviders/manager

Easily add new or override built-in providers in Laravel Socialite.

42442.0M544](/packages/socialiteproviders-manager)[php-open-source-saver/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

8359.8M52](/packages/php-open-source-saver-jwt-auth)[josiasmontag/laravel-recaptchav3

Recaptcha V3 for Laravel package

2641.6M2](/packages/josiasmontag-laravel-recaptchav3)[olssonm/l5-very-basic-auth

Laravel stateless HTTP basic auth without the need for a database

1662.5M1](/packages/olssonm-l5-very-basic-auth)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
