A simple Laravel package for following users.
- Laravel 9 or greater.
- Laravel
User
model.
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
Follow a user
auth()->user()->follow($user);
Unfollow a user
auth()->user()->unfollow($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
@if (auth()->user()->isFollowedBy($user))
This user is following you.
@endif
Returns the users a user is following
auth()->user()->getFollowing();
Returns the users who are following a user
auth()->user()->getFollowers();
Returns the most recent users who are following a user
// default limit is 5
auth()->user()->getLatestFollowers($limit);
Returns an array of IDs of the users a user is following
auth()->user()->getFollowingIds();
Returns an array of IDs of the users who are following a user
auth()->user()->getFollowersIds();
Returns an array of IDs of the users a user is following, and who is following a user
auth()->user()->getFollowingAndFollowersIds()
Caches the IDs of the users a user is following. Default is 1 day.
// 1 day
auth()->user()->cacheFollowing();
// 1 hour
auth()->user()->cacheFollowing(3600);
// 1 month
auth()->user()->cacheFollowing(Carbon::addMonth());
Returns an array of IDs of the users a user is following.
auth()->user()->getFollowingCache();
Caches the IDs of the users who are following a user. Default is 1 day.
auth()->user()->cacheFollowers();
Returns an array of IDs of the users who are following a user.
auth()->user()->getFollowersCache();
Clears the Following cache
auth()->user()->clearFollowingCache();
Clears the Followers cache
auth()->user()->clearFollowersCache();
Please see the changelog for more information on what has changed recently.
$ composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
MIT. Please see the license file for more information.