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

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

overtrue/laravel-follow
=======================

User follow unfollow system for Laravel.

6.0.0(3mo ago)1.2k424.6k↓36.6%134[1 PRs](https://github.com/overtrue/laravel-follow/pulls)5MITPHPPHP ^8.3CI failing

Since Nov 11Pushed 1mo ago22 watchersCompare

[ Source](https://github.com/overtrue/laravel-follow)[ Packagist](https://packagist.org/packages/overtrue/laravel-follow)[ GitHub Sponsors](https://github.com/overtrue)[ RSS](/packages/overtrue-laravel-follow/feed)WikiDiscussions 5.x Synced 4d ago

READMEChangelog (10)Dependencies (13)Versions (53)Used By (5)

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

[](#laravel-follow)

User follow unfollow system for Laravel.

[![Latest Stable Version](https://camo.githubusercontent.com/71cc1eca2202f6bf9ca9fb4eebd1fb050d0b601e7a2b3946661b741fcba6f249/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d666f6c6c6f772f762f737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-follow)[![Latest Unstable Version](https://camo.githubusercontent.com/e38875130ff4d49c125eb299d634f0707d73779c7c092c150afeb229adf45e4d/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d666f6c6c6f772f762f756e737461626c652e737667)](https://packagist.org/packages/overtrue/laravel-follow)[![Build Status](https://camo.githubusercontent.com/70012871b1f3519fcd34135f898c3c690cf9212117f3681076f79a4ac410ff84/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f6c61726176656c2d666f6c6c6f772f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/laravel-follow/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/692c5bfcace74033c36ec7dbe556a2414824f74b055be2f34e2299f7ce1a643a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f6c61726176656c2d666f6c6c6f772f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/laravel-follow/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/31d8c72d9d58e17500daa8919681b4291aacd7a438ff44ef311fb2aa73dad722/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d666f6c6c6f772f646f776e6c6f616473)](https://packagist.org/packages/overtrue/laravel-follow)[![License](https://camo.githubusercontent.com/66bd0ede32b58beacb2660d3542e3c6bfe9944c7c278b2962e0b7483d3b94a83/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f6c61726176656c2d666f6c6c6f772f6c6963656e7365)](https://packagist.org/packages/overtrue/laravel-follow)

Related projects:

- Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)
- Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)
- Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)
- Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)

Installing
----------

[](#installing)

```
composer require overtrue/laravel-follow -vvv
```

### Configuration and Migrations

[](#configuration-and-migrations)

```
php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider"
```

Usage
-----

[](#usage)

### Traits

[](#traits)

#### `Overtrue\LaravelFollow\Traits\Follower`

[](#overtruelaravelfollowtraitsfollower)

Add the Follower trait to your user model:

```
use Overtrue\LaravelFollow\Traits\Follower;

class User extends Authenticatable
{
    use Follower;

}
```

#### `Overtrue\LaravelFollow\Traits\Followable`

[](#overtruelaravelfollowtraitsfollowable)

Then add the Followable trait to your followable model, for example `App\User`:

```
use Overtrue\LaravelFollow\Traits\Followable;

class User extends Authenticatable
{
    use Followable;

}
```

or any other model:

```
use Overtrue\LaravelFollow\Traits\Followable;

class Channel extends Model
{
    use Followable;

}
```

### API

[](#api)

```
$user1 = User::find(1);
$user2 = User::find(2);

$user1->follow($user2);
$user1->unfollow($user2);
$user1->toggleFollow($user2);
$user1->acceptFollowRequestFrom($user2);
$user1->rejectFollowRequestFrom($user2);

$user1->isFollowing($user2);
$user2->isFollowedBy($user1);
$user2->hasRequestedToFollow($user1);
```

#### Get followings:

[](#get-followings)

```
$user->followings;
$user->approvedFollowings;
$user->notApprovedFollowings;
```

```
foreach($user->followings()->with('followable')->get() as $following)
{
    $following->created_at; // followed at

    $following->followable->nickname; // the user attributes
}
```

#### Get followers:

[](#get-followers)

```
$user->followers;
$user->approvedFollowers;
$user->notApprovedFollowers;
```

```
foreach($user->followers()->with('followers')->get() as $follower)
{
    $follower->created_at; // followed at

    $follower->follower->nickname; // the user attributes
}
```

### Follow Requests

[](#follow-requests)

If you would like to have some follow requests to need to be accepted by the user being followed, simply override the **needsToApproveFollowRequests()** method in the model that uses the **Followable** trait with your custom logic:

```
public function needsToApproveFollowRequests()
{
    // Your custom logic here
    return (bool) $this->private;
}
```

### Aggregations

[](#aggregations)

```
// followings count
$user->followings()->count();
$user->approvedFollowings()->count();
$user->notApprovedFollowings()->count();

// with query where
$user->followings()->where('gender', 'female')->count();

// followers count
$user->followers()->count();
$user->approvedFollowers()->count();
$user->notApprovedFollowers()->count();
```

List with `*_count` attribute:

```
$users = User::withCount(['followings', 'followables'])->get();
// or
$users = User::withCount(['approvedFollowings', 'approvedFollowers'])->get();

foreach($users as $user) {
    // $user->followings_count;
    // $user->followers_count;
    // or
    // $user->approved_followings_count;
    // $user->approved_followers_count;
}
```

### Attach user follow status to followable collection

[](#attach-user-follow-status-to-followable-collection)

You can use `Follower::attachFollowStatus(Collection $followables)` to attach the user follow status, it will set `has_followed` attribute to each model of `$followables`:

#### For model

[](#for-model)

```
$user1 = User::find(1);

$user->attachFollowStatus($user1);

// result
[
    "id" => 1
    "name" => "user1"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true
  ]
```

#### For `Collection | Paginator | LengthAwarePaginator | array`:

[](#for-collection--paginator--lengthawarepaginator--array)

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

$users = User::oldest('id')->get();

$users = $user->attachFollowStatus($users);

$users = $users->toArray();

// result
[
  [
    "id" => 1
    "name" => "user1"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true
  ],
  [
    "id" => 2
    "name" => "user2"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => true
  ],
  [
    "id" => 3
    "name" => "user3"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => false
  ],
  [
    "id" => 4
    "name" => "user4"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_followed" => false
  ],
]
```

#### For pagination

[](#for-pagination)

```
$users = User::paginate(20);

$user->attachFollowStatus($users);
```

### Order by followers count

[](#order-by-followers-count)

You can query users order by followers count with following methods:

- `orderByFollowersCountDesc()`
- `orderByFollowersCountAsc()`
- `orderByFollowersCount(string $direction = 'desc')`

example:

```
$users = User::orderByFollowersCountDesc()->get();
$mostPopularUser = User::orderByFollowersCountDesc()->first();
```

### N+1 issue

[](#n1-issue)

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the `with` method:

```
$users = User::with('followings')->get();

foreach($users as $user) {
    $user->isFollowing(2);
}

$users = User::with('followables')->get();

foreach($users as $user) {
    $user->isFollowedBy(2);
}
```

### Events

[](#events)

**Event****Description**`Overtrue\LaravelFollow\Events\Followed`Triggered when the relationship is created.`Overtrue\LaravelFollow\Events\Unfollowed`Triggered when the relationship is deleted.❤️ Sponsor me
-------------

[](#heart-sponsor-me)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)

如果你喜欢我的项目并想支持它，[点击这里 ❤️](https://github.com/sponsors/overtrue)

Project supported by JetBrains
------------------------------

[](#project-supported-by-jetbrains)

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

[![](https://camo.githubusercontent.com/3cf726e7cdadba47755b7f7ea4227945a92a2fa48aadf4a2573140ec6501c989/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a625f6265616d2e737667)](https://www.jetbrains.com/?from=https://github.com/overtrue)

Contributing
------------

[](#contributing)

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-follow/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-follow/issues).
3. Contribute new features or update the wiki.

*The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.*

PHP 扩展包开发
---------

[](#php-扩展包开发)

> 想知道如何从零开始构建 PHP 扩展包？
>
> 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)

License
-------

[](#license)

MIT

###  Health Score

73

—

ExcellentBetter than 100% of packages

Maintenance85

Actively maintained with recent releases

Popularity61

Solid adoption and visibility

Community39

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 76.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 ~68 days

Recently: every ~196 days

Total

52

Last Release

59d ago

Major Versions

2.4.0 → 3.0.02021-06-08

2.4.5 → 3.1.02021-11-04

3.1.1 → 4.0.02022-02-10

4.x-dev → 5.0.02022-05-17

5.3.0 → 6.0.02026-03-19

PHP version history (4 changes)1.0PHP &gt;=5.4

1.1.0PHP &gt;=5.5.9

1.1.8PHP &gt;=7.0

6.0.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (192 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (6 commits)")[![Mombuyish](https://avatars.githubusercontent.com/u/8007787?v=4)](https://github.com/Mombuyish "Mombuyish (6 commits)")[![p3yman](https://avatars.githubusercontent.com/u/2673262?v=4)](https://github.com/p3yman "p3yman (4 commits)")[![vincentvankekerix](https://avatars.githubusercontent.com/u/32239111?v=4)](https://github.com/vincentvankekerix "vincentvankekerix (4 commits)")[![kevincobain2000](https://avatars.githubusercontent.com/u/629055?v=4)](https://github.com/kevincobain2000 "kevincobain2000 (3 commits)")[![michavie](https://avatars.githubusercontent.com/u/39144548?v=4)](https://github.com/michavie "michavie (3 commits)")[![jcc](https://avatars.githubusercontent.com/u/12684082?v=4)](https://github.com/jcc "jcc (3 commits)")[![JimChenWYU](https://avatars.githubusercontent.com/u/12371374?v=4)](https://github.com/JimChenWYU "JimChenWYU (3 commits)")[![CasperLaiTW](https://avatars.githubusercontent.com/u/5094008?v=4)](https://github.com/CasperLaiTW "CasperLaiTW (3 commits)")[![ams-ryanolson](https://avatars.githubusercontent.com/u/46386725?v=4)](https://github.com/ams-ryanolson "ams-ryanolson (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (2 commits)")[![thisliu](https://avatars.githubusercontent.com/u/29086766?v=4)](https://github.com/thisliu "thisliu (2 commits)")[![YL20181120](https://avatars.githubusercontent.com/u/5860349?v=4)](https://github.com/YL20181120 "YL20181120 (1 commits)")[![AdrianHL](https://avatars.githubusercontent.com/u/16022257?v=4)](https://github.com/AdrianHL "AdrianHL (1 commits)")[![zombozo12](https://avatars.githubusercontent.com/u/21243980?v=4)](https://github.com/zombozo12 "zombozo12 (1 commits)")[![ahmedessa315](https://avatars.githubusercontent.com/u/84974296?v=4)](https://github.com/ahmedessa315 "ahmedessa315 (1 commits)")[![buzzclue](https://avatars.githubusercontent.com/u/43321373?v=4)](https://github.com/buzzclue "buzzclue (1 commits)")[![captenmasin](https://avatars.githubusercontent.com/u/25454872?v=4)](https://github.com/captenmasin "captenmasin (1 commits)")

---

Tags

favoritefollowfollowerslaravellikesubscribe

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[jeremy379/laravel-openid-connect

OpenID Connect support to the PHP League's OAuth2 Server. Compatible with Laravel Passport.

59437.0k9](/packages/jeremy379-laravel-openid-connect)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

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