A simple Laravel package for following users.
- PHP 8.3 or greater
- Laravel 12 or greater
Via Composer
$ composer require timgavin/laravel-followImport 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
Publish the config file.
php artisan vendor:publish --tag=laravel-follow-configAvailable options:
return [
'cache_duration' => 60 * 60 * 24, // 24 hours in seconds
'dispatch_events' => true,
'user_model' => null, // falls back to auth config
];Returns true if the user was followed, false if already following.
auth()->user()->follow($user);Returns true if the user was unfollowed, false if not following.
auth()->user()->unfollow($user);Returns true if now following, false if unfollowed.
auth()->user()->toggleFollow($user);@if (auth()->user()->isFollowing($user))
You are following this user.
@endif@if (auth()->user()->isFollowedBy($user))
This user is following you.
@endif@if (auth()->user()->isMutuallyFollowing($user))
You follow each other.
@endif@if (auth()->user()->hasAnyFollowWith($user))
There is a follow relationship.
@endifauth()->user()->getFollowingCount();auth()->user()->getFollowersCount();auth()->user()->getFollowing();auth()->user()->getFollowingPaginated(15);auth()->user()->getFollowers();auth()->user()->getFollowersPaginated(15);// default limit is 5
auth()->user()->getLatestFollowers($limit);auth()->user()->getFollowingIds();auth()->user()->getFollowersIds();auth()->user()->getFollowingAndFollowersIds();Returns IDs of users you're following AND users following you.
auth()->user()->getAllFollowUserIds();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]]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]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();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);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 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 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();If upgrading from 1.x, please see the upgrade guide.
Please see the changelog for more information on what has changed recently.
$ composer testIf you discover any security related issues, please email tim@timgavin.me instead of using the issue tracker.
MIT. Please see the license file for more information.