This package gives Eloqent models the ability to manage their friendships. You can easily design a Facebook like Friend System.
##Models can:
- Send Friend Requests
- Accept Friend Requests
- Deny Friend Requests
- Block Another Model
First, install the package through Composer.
composer require hootlex/laravel-friendships
Then include the service provider inside config/app.php
.
'providers' => [
...
Hootlex\Friendships\FriendshipsServiceProvider::class,
...
];
Lastly you need to publish the migration and migrate the database
php artisan vendor:publish --provider="Hootlex\Friendships\FriendshipsServiceProvider" && php artisan migrate
use Hootlex\Friendships\Traits\Friendable;
class User extends Model
{
use Friendable;
...
}
Check the Test file to see the package in action
$user->befriend($recipient);
$user->acceptFriendRequest($recipient);
$user->denyFriendRequest($recipient);
$user->unfriend($recipient);
$user->blockFriend($recipient);
$user->unblockFriend($recipient);
$user->isFriendWith($recipient);
$user->hasBlocked($recipient);
$user->isBlockedBy($recipient);
Get All Friends(accepted) (It returns a collection of friend models not friendships, for example User)
$user->getAllFriends();
$user->getFriendsLimited($limit);
Default $limit = 0, will return all friends
$user->getFriendsLimited(5);
This will return only 5 friends.
$user->getFriendsWithPagination($perPage);
Default $perPage = 0, will return all friends
$friends = $user->getFriendsWithPagination(5);
This will return a paginator object. To render links
Laravel >5.0: Use ->render();
Laravel >5.2: Use ->links();
@foreach($friends as $friend)
<p>{{ $friend->name }}</p>
@endforeach
{!! $friends->links() !!}
$user->getFriendship($recipient);
$user->getAllFriendships();
$user->getPendingFriendships();
$user->getAcceptedFriendships();
$user->getDeniedFriendships();
$user->getBlockedFriendships();
$user->getFriendRequests();