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

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

timgavin/laravel-follow
=======================

A User can follow another User

v2.2.0(5mo ago)34721MITPHPPHP ^8.3CI passing

Since Oct 3Pushed 5mo ago1 watchersCompare

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

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

Laravel Follow
==============

[](#laravel-follow)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1cb312d0b6e756424600fa2364c0d83c5c23ed2bffda5c1a572a11171a85a318/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74696d676176696e2f6c61726176656c2d666f6c6c6f772e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timgavin/laravel-follow)[![Total Downloads](https://camo.githubusercontent.com/a4302c72dd767b506d1492ebaae44be92331d6116e43fd4baaca177e0859f6a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74696d676176696e2f6c61726176656c2d666f6c6c6f772e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timgavin/laravel-follow)[![Tests](https://camo.githubusercontent.com/5486c850123637d01150cb27285cad51cf69fb823c60adfe2bac5c97c3e447a1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74696d676176696e2f6c61726176656c2d666f6c6c6f772f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/timgavin/laravel-follow/actions/workflows/tests.yml)

A simple Laravel package for following users.

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

[](#requirements)

- PHP 8.3 or greater
- Laravel 12 or greater

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

[](#installation)

Via Composer

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

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

```
namespace App\Models;

use TimGavin\LaravelFollow\LaravelFollow;

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

Then run migrations.

```
php artisan migrate

```

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

[](#configuration)

Publish the config file.

```
php artisan vendor:publish --tag=laravel-follow-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)

### Follow a user

[](#follow-a-user)

Returns `true` if the user was followed, `false` if already following.

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

### Unfollow a user

[](#unfollow-a-user)

Returns `true` if the user was unfollowed, `false` if not following.

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

### Toggle follow

[](#toggle-follow)

Returns `true` if now following, `false` if unfollowed.

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

### Check if a user is following another user

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

```
@if (auth()->user()->isFollowing($user))
    You are following this user.
@endif
```

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

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

```
@if (auth()->user()->isFollowedBy($user))
    This user is following you.
@endif
```

### Check if users are mutually following each other

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

```
@if (auth()->user()->isMutuallyFollowing($user))
    You follow each other.
@endif
```

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

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

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

### Get following count

[](#get-following-count)

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

### Get followers count

[](#get-followers-count)

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

### Get the users a user is following

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

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

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

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

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

### Get the users who are following a user

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### Get an array of IDs of both following and followers

[](#get-an-array-of-ids-of-both-following-and-followers)

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

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

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

Returns IDs of users you're following AND users following you.

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

### Get follow status for multiple users in batch

[](#get-follow-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()->getFollowStatusForUsers($userIds);

// Returns: [userId => ['is_following' => bool, 'is_followed_by' => bool]]
```

### Get follow status for a single user

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

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

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

// Returns: ['is_following' => bool, 'is_followed_by' => bool]
```

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

[](#query-scopes)

### Exclude follow-related users from queries

[](#exclude-follow-related-users-from-queries)

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

```
// Exclude users following or followed by the authenticated user
User::query()->excludeFollowRelated()->get();

// Exclude users following or followed by a specific user
User::query()->excludeFollowRelated($user)->get();
```

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

[](#relationships)

Access the follows relationship (users this user is following).

```
$user->follows;
```

Access the followers relationship (users following this user).

```
$user->followers;
```

Get the follow relationship record where this user follows another.

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

Get the follow relationship record where another user follows this user.

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

Get all follow relationships between two users.

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

Caching
-------

[](#caching)

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

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

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

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

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

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

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

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

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

Clear the Following cache.

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

Clear the Followers cache.

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

Clear the Followers cache for another user. Useful after following a user to keep their followers cache in sync.

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

Clear the Following cache for another user.

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

Note: The cache is automatically cleared when calling `follow()` or `unfollow()`. However, only the current user's cache is cleared. Use `clearFollowersCacheFor()` to clear the target user's followers cache if needed.

Events
------

[](#events)

Events are dispatched when users follow or unfollow each other.

```
use TimGavin\LaravelFollow\Events\UserFollowed;
use TimGavin\LaravelFollow\Events\UserUnfollowed;

Event::listen(UserFollowed::class, function ($event) {
    // $event->userId - the user who followed
    // $event->followingId - the user who was followed
});

Event::listen(UserUnfollowed::class, function ($event) {
    // $event->userId - the user who unfollowed
    // $event->unfollowedId - the user who was unfollowed
});
```

Disable events in config.

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

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

[](#query-scopes-1)

Query scopes are available on the Follow model.

```
use TimGavin\LaravelFollow\Models\Follow;

// Get follows where a user is following others
Follow::whereUserFollows($userId)->get();

// Get follows where a user is being followed
Follow::whereUserIsFollowedBy($userId)->get();

// Get all follows involving a user
Follow::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

46

—

FairBetter than 93% of packages

Maintenance73

Regular maintenance activity

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 95.1% 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

153d 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 (39 commits)")[![zer05media](https://avatars.githubusercontent.com/u/201400387?v=4)](https://github.com/zer05media "zer05media (2 commits)")

---

Tags

followfollowinglaravelphp8laravelLaravelFollowFollow users

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

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

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

###  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)
