How to add Friendship system with Eloquent in laravel

So , you want to create Social networking site or something like facebook ?*( btw i have created one hyguyz.com ) , i have done all the stuffs manually , but you can do that with ease , so first important thing you want to add in your social network is friendship system  , Right ? but you dont have to write codes  manually  because Laravel 5 Friendships packaage will save your lot of time ! This package gives Eloqent models the ability to manage their friendships. your Models can:

  • Send Friend Requests
  • Accept Friend Requests
  • Deny Friend Requests
  • Block Another Model

so lets get started with installation

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


now we will set up our model by adding trait

use Hootlex\Friendships\Traits\Friendable;
class User extends Model
{
    use Friendable;
    ...
}


usage is very simple …

Send a Friend Request

$user->befriend($recipient);

Accept a Friend Request

$user->acceptFriendRequest($recipient);

Deny a Friend Request

$user->denyFriendRequest($recipient);

Remove Friend

$user->unfriend($recipient);

Block a Model

$user->blockFriend($recipient);

Unblock a Model

$user->unblockFriend($recipient);

Check if Model is Friend with another Model

$user->isFriendWith($recipient);

Check if Model has blocked another Model

$user->hasBlocked($recipient);

Check if Model is blocked by another Model

$user->isBlockedBy($recipient);

Get a single friendship

$user->getFriendship($recipient);

Get a list of all Friendships

$user->getAllFriendships();

Get a list of pending Friendships

$user->getPendingFriendships();

Get a list of accepted Friendships

$user->getAcceptedFriendships();

Get a list of denied Friendships

$user->getDeniedFriendships();

Get a list of blocked Friendships

$user->getBlockedFriendships();

Get a list of pending Friend Requests

$user->getFriendRequests();




so overall your code wwill look like below

 
<?php use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class FriedshipsTest extends TestCase { use DatabaseTransactions; /** @test */ public function user_can_send_a_friend_request() { $sender = createUser(); $recipient = createUser(); $sender->befriend($recipient);
        $this->assertCount(1, $recipient->getFriendRequests());
    }
    /** @test */
    public function user_can_not_send_a_friend_request_if_frienship_is_pending()
    {
        $sender = createUser();
        $recipient = createUser();
        $sender->befriend($recipient);
        $sender->befriend($recipient);
        $sender->befriend($recipient);
        $this->assertCount(1, $recipient->getFriendRequests());
    }
    /** @test */
    public function user_is_friend_with_another_user_if_accepts_a_friend_request()
    {
        $sender = createUser();
        $recipient = createUser();
        //send fr
        $sender->befriend($recipient);
        //accept fr
        $recipient->acceptFriendRequest($sender);
        $this->assertTrue($recipient->isFriendWith($sender));
        //fr has been delete
        $this->assertCount(0, $recipient->getFriendRequests());
    }
    /** @test */
    public function user_is_not_friend_with_another_user_until_he_accepts_a_friend_request()
    {
        $sender = createUser();
        $recipient = createUser();
        //send fr
        $sender->befriend($recipient);
        $this->assertFalse($recipient->isFriendWith($sender));
    }
    /** @test */
    public function user_can_deny_a_friend_request()
    {
        $sender = createUser();
        $recipient = createUser();
        $sender->befriend($recipient);
        $recipient->denyFriendRequest($sender);
        $this->assertFalse($recipient->isFriendWith($sender));
        //fr has been delete
        $this->assertCount(0, $recipient->getFriendRequests());
        $this->assertCount(1, $sender->getDeniedFriendships());
    }
    /** @test */
    public function user_can_block_another_user(){
        $sender = createUser();
        $recipient = createUser();
        $sender->blockFriend($recipient);
        $this->assertTrue($recipient->isBlockedBy($sender));
        $this->assertTrue($sender->hasBlocked($recipient));
        //sender is not blocked by receipient
        $this->assertFalse($sender->isBlockedBy($recipient));
        $this->assertFalse($recipient->hasBlocked($sender));
    }
    /** @test */
    public function user_can_unblock_a_blocked_user(){
        $sender = createUser();
        $recipient = createUser();
        $sender->blockFriend($recipient);
        $sender->unblockFriend($recipient);
        $this->assertFalse($recipient->isBlockedBy($sender));
        $this->assertFalse($sender->hasBlocked($recipient));
    }
    /** @test */
    public function it_returns_all_user_friendships(){
        $sender = createUser();
        $recipients = createUser([], 3);
        foreach ($recipients as $recipient) {
            $sender->befriend($recipient);
        }
        $recipients[0]->acceptFriendRequest($sender);
        $recipients[1]->acceptFriendRequest($sender);
        $recipients[2]->denyFriendRequest($sender);
        $this->assertCount(3, $sender->getAllFriendships());
    }
    /** @test */
    public function it_returns_accepted_user_friendships(){
        $sender = createUser();
        $recipients = createUser([], 3);
        foreach ($recipients as $recipient) {
            $sender->befriend($recipient);
        }
        $recipients[0]->acceptFriendRequest($sender);
        $recipients[1]->acceptFriendRequest($sender);
        $recipients[2]->denyFriendRequest($sender);
        $this->assertCount(2, $sender->getAcceptedFriendships());
    }
    /** @test */
    public function it_returns_pending_user_friendships(){
        $sender = createUser();
        $recipients = createUser([], 3);
        foreach ($recipients as $recipient) {
            $sender->befriend($recipient);
        }
        $recipients[0]->acceptFriendRequest($sender);
        $this->assertCount(2, $sender->getPendingFriendships());
    }
    /** @test */
    public function it_returns_denied_user_friendships(){
        $sender = createUser();
        $recipients = createUser([], 3);
        foreach ($recipients as $recipient) {
            $sender->befriend($recipient);
        }
        $recipients[0]->acceptFriendRequest($sender);
        $recipients[1]->acceptFriendRequest($sender);
        $recipients[2]->denyFriendRequest($sender);
        $this->assertCount(1, $sender->getDeniedFriendships());
    }
    /** @test */
    public function it_returns_blocked_user_friendships(){
        $sender = createUser();
        $recipients = createUser([], 3);
        foreach ($recipients as $recipient) {
            $sender->befriend($recipient);
        }
        $recipients[0]->acceptFriendRequest($sender);
        $recipients[1]->acceptFriendRequest($sender);
        $recipients[2]->blockFriend($sender);
        $this->assertCount(1, $sender->getBlockedFriendships());
    }
}



so you are learning laravel or new to laravel ?if yes then you can now deploy your laravel app in cloud for free Go and get $50 credit to test your laravel app live in Action

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Get The best In Web dev

Get The best In Web dev

Join dunebook and recieve best in Angular, React, Vue, Python, Java & more. Directly to your inbox once a week FREE !

You have Successfully Subscribed!

Pin It on Pinterest