This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.
- Send Friend Requests
- Accept Friend Requests
- Deny Friend Requests
- Block Another Model
- Group Friends
First, install the package through Composer.
composer require shababsoftwares/laravel-friendships
Publish config and migrations
php artisan vendor:publish --provider="ShababSoftwares\Friendships\FriendshipsServiceProvider"
Configure the published config in
config\friendships.php
Finally, migrate the database
php artisan migrate
use ShababSoftwares\Friendships\Traits\Friendable;
class User extends Model
{
use Friendable;
...
}
Check the Test file to see the package in action
$user->befriend($recipient);
$user->acceptFriendRequest($sender);
$user->denyFriendRequest($sender);
$user->unfriend($friend);
$user->blockFriend($friend);
$user->unblockFriend($friend);
$user->isFriendWith($friend);
$user->hasFriendRequestFrom($sender);
$user->hasSentFriendRequestTo($recipient);
$user->hasBlocked($friend);
$user->isBlockedBy($friend);
$user->getFriendship($friend);
$user->getAllFriendships();
$user->getPendingFriendships();
$user->getAcceptedFriendships();
$user->getDeniedFriendships();
$user->getBlockedFriendships();
$user->getFriendRequests();
$user->getFriendsCount();
$user->getPendingsCount();
$user->getMutualFriendsCount($otherUser);
To get a collection of friend models (ex. User) use the following methods:
$user->getFriends();
$user->getFriends($perPage = 20);
$user->getFriendsOfFriends($perPage = 20);
$user->getFriends($perPage = 20, $group_name);
$user->getMutualFriends($otherUser, $perPage = 20);
// Methods usages (Status available: pending, denied, blocked and accepted.) (Paginators available: none, default, simple)
$user->{status}Friends($resultsPerPage = 0, $paginationType = 'none');
// Example #1: (Get accepted friends using default paginator with 25 results per page).
$user->acceptedFriends(25, 'default');
// Example #2: (Get pending friends using simple paginator with 10 results per page).
$user->pendingFriends(10, 'simple');
// Example #3: (Get all denied friends without pagination).
$user->deniedFriends();
// Example #3: (Get denied friends using default paginator with 30 results per page).
$user->blockedFriends(30);
The friend groups are defined in the config/friendships.php
file.
The package comes with a few default groups.
To modify them, or add your own, you need to specify a slug
and a key
.
// config/friendships.php
...
'groups' => [
'acquaintances' => 0,
'close_friends' => 1,
'family' => 2
]
Since you've configured friend groups, you can group/ungroup friends using the following methods.
$user->groupFriend($friend, $group_name);
$user->ungroupFriend($friend, 'family');
$user->ungroupFriend($friend);
$user->getFriendsCount($group_name);
$user->getAllFriendships($group_name);
$user->getAcceptedFriendships($group_name);
$user->getPendingFriendships($group_name);
...
This is the list of the events fired by default for each action
Event name | Fired |
---|---|
ShababSoftwares\Friendships\Events\Sent | When a friend request is sent |
ShababSoftwares\Friendships\Events\Accepted | When a friend request is accepted |
ShababSoftwares\Friendships\Events\Denied | When a friend request is denied |
ShababSoftwares\Friendships\Events\Blocked | When a friend is blocked |
ShababSoftwares\Friendships\Events\Unblocked | When a friend is unblocked |
ShababSoftwares\Friendships\Events\Cancelled | When a friendship is cancelled |
The MIT License (MIT). Please see LICENSE for more information.
Shabab Softwares (c) 2024