Gives Eloquent models the ability to manage their followers.
- Send Follow Requests
- Accept Follow Requests
- Deny Follow Requests
- Block Another Model
First, install the package through Composer.
composer require skybluesofa/laravel-followers
Then include the service provider inside config/app.php
.
'providers' => [
...
Skybluesofa\Followers\ServiceProvider::class,
...
];
Publish config and migrations
php artisan vendor:publish --provider="Skybluesofa\Followers\ServiceProvider"
Configure the published config in
config\followers.php
Finally, migrate the database
php artisan migrate
use Skybluesofa\Followers\Traits\Followable;
class User extends Model
{
use Followable;
...
}
Check the Test file to see the package in action
Will trigger a Skybluesofa\LaravelFollowers\Events\FollowRequest
event.
$user->follow($recipient);
Will trigger a Skybluesofa\LaravelFollowers\Events\FollowRequestAccepted
event.
$recipient->acceptFollowRequestFrom($user);
Will trigger a Skybluesofa\LaravelFollowers\Events\FollowRequestDenied
event.
$recipient->denyFollowRequestFrom($user);
Will trigger a Skybluesofa\LaravelFollowers\Events\Unfollow
event.
$user->unfollow($recipient);
Will trigger a Skybluesofa\LaravelFollowers\Events\FollowingBlocked
event.
$user->blockBeingFollowedBy($recipient);
Will trigger a Skybluesofa\LaravelFollowers\Events\FollowingUnblocked
event.
$user->unblockBeingFollowedBy($recipient);
$user->isFollowing($recipient);
$recipient->isFollowedBy($user);
$recipient->hasFollowRequestFrom($user);
$user->hasSentFollowRequestTo($recipient);
$recipient->hasBlockedBeingFollowedBy($user);
$user->isBlockedFromFollowing($recipient);
$user->getFriendship($recipient);
$user->getAllFriendships();
$user->getPendingFriendships();
$user->getAcceptedFriendships();
$user->getDeniedFriendships();
$user->getBlockedFriendships();
$user->getFriendRequests();
$user->getFriendsCount();
To get a collection of friend models (ex. User) use the following methods:
$user->getFriends();
$user->getFriends($perPage = 20);
These events are triggered during the lifecycle of following/unfollowing/accept/deny followers:
Skybluesofa\LaravelFollowers\Events\FollowingBlocked(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowingUnblocked(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequest(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequestAccepted(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\FollowRequestDenied(Model $recipient, Model $sender);
Skybluesofa\LaravelFollowers\Events\Unfollow(Model $recipient, Model $sender);
To listen for and react to these events, follow the instructions available in the Laravel Documentation.
The basis of this code was garnered from https://github.com/hootlex/laravel-friendships. Although it was a jumping off point, much of the code has been rewritten to allow for Following as opposed to Mutual Friendship.
See the CONTRIBUTING guide.