hootlex / laravel-friendships

This package gives Eloquent models the ability to manage their friendships.
MIT License
706 stars 151 forks source link

Friendship Status #121

Closed elephantux closed 5 years ago

elephantux commented 5 years ago

How to get friendship status types? pending, accepted, declined etc.? I need show table with all Friendships and show status of each.

TechTailor commented 5 years ago

Hey,

You can use the getAllFriendships() method which returns all types of friendships.

You can then get the status of the friendship (0=Pending, 1=Accepted, 2=Denied, 3=Blocked) and eager load the sender/recipient details (username, email, etc) and the filter through the collection to display the results as desired.

A very basic way of testing the same would be -

$user = Auth::user();
$friends = $user->getAllFriendships(); 
foreach($friends as $friend)
{
    echo 'username = ' . $friend->sender->username; (or $friend->recipient->username)

    if($friend->status == 0)
        echo 'status = PENDING';
    else if($friend->status == 1)
        echo 'status = ACCEPTED';
    else if($friend->status == 2)
        echo 'status = DENIED';
    else if($friend->status == 3)
        echo 'status = BLOCKED';

}

Now there is a limitation here which unfortunately we cannot get fixed since it seems this package is no longer being maintained (or accepting PR's) is that the methods will return the details of the requesting user itself which leads to a mess and confusion between friend requests sent and friend requests received by the user.

Hence why I personally whenever in use of this package, run multiple loops i.e., instead of using getAllFriendships(), I make use of the combination of getAcceptedFriendships(), getDeniedFriendships(), getBlockedFriendships(), etc. (not very efficient I know, but thats what I got to work with until I can fix this with tests).

If anyone has a better way to of doing it, please share here or better create a PR.

Hope this helps.

elephantux commented 5 years ago

Thanks @TechTailor

I was looking for exactly this: 0=Pending, 1=Accepted, 2=Denied, 3=Blocked