PHPackages                             timgavin/laravel-block - 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. timgavin/laravel-block

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

timgavin/laravel-block
======================

A User can block another User

v2.2.0(5mo ago)41.6kMITPHPPHP ^8.3CI passing

Since Oct 3Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/timgavin/laravel-block)[ Packagist](https://packagist.org/packages/timgavin/laravel-block)[ Docs](https://github.com/timgavin/laravel-block)[ RSS](/packages/timgavin-laravel-block/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (17)Used By (0)

Laravel Block
=============

[](#laravel-block)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8aa961e4964b366fe03c9855273cc798388ab4bfe850627ba116b8b06b3176ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74696d676176696e2f6c61726176656c2d626c6f636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timgavin/laravel-block)[![Total Downloads](https://camo.githubusercontent.com/5339037ae59cfd71f5a2cc92fe8a6d7a0476809dcac0472ca5579a1c015415b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74696d676176696e2f6c61726176656c2d626c6f636b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timgavin/laravel-block)[![Tests](https://camo.githubusercontent.com/2538db56bbe1de2262ea5a5a12dc67d442671fd1d10bc9999271fbb00a7f826f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74696d676176696e2f6c61726176656c2d626c6f636b2f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/timgavin/laravel-block/actions/workflows/tests.yml)

A simple Laravel package for blocking users.

Requirements
------------

[](#requirements)

- PHP 8.3 or greater
- Laravel 12 or greater

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

[](#installation)

Via Composer

```
$ composer require timgavin/laravel-block
```

Import Laravel Block into your User model and add the trait.

```
namespace App\Models;

use TimGavin\LaravelBlock\LaravelBlock;

class User extends Authenticatable
{
    use LaravelBlock;
}
```

Then run migrations.

```
php artisan migrate

```

Configuration
-------------

[](#configuration)

Publish the config file.

```
php artisan vendor:publish --tag=laravel-block-config
```

Available options:

```
return [
    'cache_duration' => 60 * 60 * 24, // 24 hours in seconds
    'dispatch_events' => true,
    'user_model' => null, // falls back to auth config
];
```

Usage
-----

[](#usage)

### Block a user

[](#block-a-user)

Returns `true` if the user was blocked, `false` if already blocking.

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

### Unblock a user

[](#unblock-a-user)

Returns `true` if the user was unblocked, `false` if not blocking.

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

### Toggle block

[](#toggle-block)

Returns `true` if now blocking, `false` if unblocked.

```
auth()->user()->toggleBlock($user);
```

### Check if a user is blocking another user

[](#check-if-a-user-is-blocking-another-user)

```
@if (auth()->user()->isBlocking($user))
    You are blocking this user.
@endif
```

### Check if a user is blocked by another user

[](#check-if-a-user-is-blocked-by-another-user)

```
@if (auth()->user()->isBlockedBy($user))
    This user is blocking you.
@endif
```

### Check if users are mutually blocking each other

[](#check-if-users-are-mutually-blocking-each-other)

```
@if (auth()->user()->isMutuallyBlocking($user))
    You are both blocking each other.
@endif
```

### Check if there is any block relationship between two users

[](#check-if-there-is-any-block-relationship-between-two-users)

```
@if (auth()->user()->hasAnyBlockWith($user))
    There is a block relationship.
@endif
```

### Get blocking count

[](#get-blocking-count)

```
auth()->user()->getBlockingCount();
```

### Get blockers count

[](#get-blockers-count)

```
auth()->user()->getBlockersCount();
```

### Get the users a user is blocking

[](#get-the-users-a-user-is-blocking)

```
auth()->user()->getBlocking();
```

### Get the users a user is blocking with pagination

[](#get-the-users-a-user-is-blocking-with-pagination)

```
auth()->user()->getBlockingPaginated(15);
```

### Get the users who are blocking a user

[](#get-the-users-who-are-blocking-a-user)

```
auth()->user()->getBlockers();
```

### Get the users who are blocking a user with pagination

[](#get-the-users-who-are-blocking-a-user-with-pagination)

```
auth()->user()->getBlockersPaginated(15);
```

### Get the most recent users who are blocking a user

[](#get-the-most-recent-users-who-are-blocking-a-user)

```
// default limit is 5
auth()->user()->getLatestBlockers($limit);
```

### Get an array of IDs of the users a user is blocking

[](#get-an-array-of-ids-of-the-users-a-user-is-blocking)

```
auth()->user()->getBlockingIds();
```

### Get an array of IDs of the users who are blocking a user

[](#get-an-array-of-ids-of-the-users-who-are-blocking-a-user)

```
auth()->user()->getBlockersIds();
```

### Get an array of IDs of both blocking and blockers

[](#get-an-array-of-ids-of-both-blocking-and-blockers)

```
auth()->user()->getBlockingAndBlockersIds();
```

### Get all user IDs involved in any block relationship (single query)

[](#get-all-user-ids-involved-in-any-block-relationship-single-query)

Useful for feed exclusion - returns IDs of users you're blocking AND users blocking you.

```
auth()->user()->getAllBlockUserIds();
```

### Get block status for multiple users in batch

[](#get-block-status-for-multiple-users-in-batch)

Returns status for multiple users in just 2 queries instead of 2N. Useful for API responses.

```
$userIds = $users->pluck('id')->toArray();
$statuses = auth()->user()->getBlockStatusForUsers($userIds);

// Returns: [userId => ['is_blocking' => bool, 'is_blocked_by' => bool]]
```

### Get block status for a single user

[](#get-block-status-for-a-single-user)

Returns bidirectional block status in a single query. Useful for profile pages.

```
$status = auth()->user()->getBlockStatusFor($user);

// Returns: ['is_blocking' => bool, 'is_blocked_by' => bool]
```

Query Scopes
------------

[](#query-scopes)

### Exclude blocked users from queries

[](#exclude-blocked-users-from-queries)

Excludes users involved in any block relationship with the given user.

```
// Exclude users blocked by or blocking the authenticated user
User::query()->excludeBlocked()->get();

// Exclude users blocked by or blocking a specific user
User::query()->excludeBlocked($user)->get();
```

Relationships
-------------

[](#relationships)

Access the blocks relationship (users this user is blocking).

```
$user->blocks;
```

Access the blockers relationship (users blocking this user).

```
$user->blockers;
```

Get the block relationship record where this user blocks another.

```
$user->getBlockingRelationship($otherUser);
```

Get the block relationship record where another user blocks this user.

```
$user->getBlockerRelationship($otherUser);
```

Get all block relationships between two users.

```
$user->getBlockRelationshipsWith($otherUser);
```

Caching
-------

[](#caching)

Cache the IDs of the users a user is blocking. Default duration is set in config.

```
auth()->user()->cacheBlocking();

// custom duration in seconds
auth()->user()->cacheBlocking(3600);
```

Get the cached IDs of the users a user is blocking.

```
auth()->user()->getBlockingCache();
```

Cache the IDs of the users who are blocking a user.

```
auth()->user()->cacheBlockers();
```

Get the cached IDs of the users who are blocking a user.

```
auth()->user()->getBlockersCache();
```

Clear the Blocking cache.

```
auth()->user()->clearBlockingCache();
```

Clear the Blockers cache.

```
auth()->user()->clearBlockersCache();
```

Clear the Blockers cache for another user. Useful after blocking a user to keep their blockers cache in sync.

```
auth()->user()->clearBlockersCacheFor($user);
```

Clear the Blocking cache for another user.

```
auth()->user()->clearBlockingCacheFor($user);
```

Note: The cache is automatically cleared when calling `block()` or `unblock()`. However, only the current user's cache is cleared. Use `clearBlockersCacheFor()` to clear the target user's blockers cache if needed.

Events
------

[](#events)

Events are dispatched when users block or unblock each other.

```
use TimGavin\LaravelBlock\Events\UserBlocked;
use TimGavin\LaravelBlock\Events\UserUnblocked;

Event::listen(UserBlocked::class, function ($event) {
    // $event->userId - the user who blocked
    // $event->blockedId - the user who was blocked
});

Event::listen(UserUnblocked::class, function ($event) {
    // $event->userId - the user who unblocked
    // $event->unblockedId - the user who was unblocked
});
```

Disable events in config.

```
'dispatch_events' => false,
```

Query Scopes
------------

[](#query-scopes-1)

Query scopes are available on the Block model.

```
use TimGavin\LaravelBlock\Models\Block;

// Get blocks where a user is blocking others
Block::whereUserBlocks($userId)->get();

// Get blocks where a user is being blocked
Block::whereUserIsBlockedBy($userId)->get();

// Get all blocks involving a user
Block::involvingUser($userId)->get();
```

Upgrading
---------

[](#upgrading)

If upgrading from 1.x, please see the [upgrade guide](UPGRADE.md).

Change log
----------

[](#change-log)

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

Testing
-------

[](#testing)

```
$ composer test
```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

MIT. Please see the [license file](license.md) for more information.

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance73

Regular maintenance activity

Popularity25

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 95.5% 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 ~77 days

Recently: every ~194 days

Total

16

Last Release

151d ago

Major Versions

1.1.10 → 2.0.02025-12-07

### Community

Maintainers

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

---

Top Contributors

[![timgavin](https://avatars.githubusercontent.com/u/1012049?v=4)](https://github.com/timgavin "timgavin (42 commits)")[![zer05media](https://avatars.githubusercontent.com/u/201400387?v=4)](https://github.com/zer05media "zer05media (2 commits)")

---

Tags

blockblockinglaravelphp8laravelLaravelBlockBlock users

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/timgavin-laravel-block/health.svg)

```
[![Health](https://phpackages.com/badges/timgavin-laravel-block/health.svg)](https://phpackages.com/packages/timgavin-laravel-block)
```

###  Alternatives

[imdhemy/laravel-purchases

The top-notch Laravel receipt validator.

3831.1M2](/packages/imdhemy-laravel-purchases)[martbock/laravel-diceware

Diceware Passphrase Generator for Laravel

3264.7k](/packages/martbock-laravel-diceware)[orchestra/auth

Auth Component for Orchestra Platform

24108.5k3](/packages/orchestra-auth)[ingria/laravel-x509-auth

Laravel 5 Client Certificate auth middleware

375.6k](/packages/ingria-laravel-x509-auth)

PHPackages © 2026

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