hootlex / laravel-friendships

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

I don't understand how to use this package. #115

Closed antweny closed 5 years ago

antweny commented 5 years ago

Im developing a proffesional network and want the users to connect to each other. I found this package but how to i use it especcialy when you already have the user model which stores all the users. hoa to get sender id and receipt it.

TechTailor commented 5 years ago

its very simple if you go through the documentation thoroughly. Just give it a few reads. For any confusion, the sender_id and recipient_id will always be the User's ID's, just changes depending who is the user sending the request and who is the user receiving the request.

For Ex: we have a table with 5 users with id's (1,2,3,4,5, respectively) - So if the User with ID 1 (who also happens to be logged-in) wants to send a friend to a User with ID 2, this is how you write it -

$user = Auth::user(); [logged-in user is the sender]
$recipient = User::where('id',2)->get(); [the user who you want to send friend request to]
$user->befriend($recipient); [here the system will automatically take the id of both the users and assign the $user->id as sender id and $recipient->id as recipient_id in the pivot table.

Hope this clears up.

-CR

antweny commented 5 years ago

its very simple if you go through the documentation thoroughly. Just give it a few reads. For any confusion, the sender_id and recipient_id will always be the User's ID, just changes depending who is the user sending the request and who is the user receiving the request.

For Ex: we have a table with 5 users with id's (1,2,3,4,5, respectively) - So if the User with ID wants to send a friend to a User with ID 2, this is how you write it -

$user = Auth::user(); [logged-in user is the sender]
$recipient = User::where('id',2)->get(); [the user who you want to send friend request to]
$user->befriend($recipient); [here the system will automatically take the id of both the users and assign the $user->id as sender id and $recipient->id as recipient_id in the pivot table.

Hope this clears up.

-CR

I'm not good on writing english hope u'll understand me. Thank you very much this is an eye open to me. I looked at the test file but i can't figure out how to pass the recipient id on where.

the test/FriendshipsTest.php file is like this if you want to send the friend requset public function user_can_send_a_friend_request() { $sender = createUser(); $recipient = createUser(); $sender->befriend($recipient); $this->assertCount(1, $recipient->getFriendRequests()); }

So mine should i change be like this or there is another way of doing this public function user_can_send_a_friend_request(Request $request) { $sender = Auth::user(); $recipient = User::where('id',$request->recipeint_id)->get();; $sender->befriend($recipient); $this->assertCount(1, $recipient->getFriendRequests()); }

where the $request->recipient_id is submited from the form like this {{ Form::open(['method' => 'POST','route' => ['friendships.sent']] )}} @csrf

{{ Form::close() }}

TechTailor commented 5 years ago

Hey, for future when writing/pasting code use the "insert code" which looks like this <>

As for your question, Tests are not designed for you, its only useful for those who knows the inner working and want to make sure if they make any changes to the code, they wont break anything. Looking at the tests without understanding the functions will not make any sense for you or anyone.

Just look at the instructions in the readme file as they are all what u will need when simply using this package.

As for your confusion, you can use a form, display the user a lists of members and who they want to be friends with, they can select one user and submit. Now you do not pass the recipient_id to the befriend function, you need to pass the recipient's user model, i will give u a little snippet to explain better -

public function sendRequest(Request $request) 
{  
$sendTo = $request->recipient_id;
$recipient = User::find($sendTo);  // Loading the User Model with the id received from the form
$user = Auth::user(); // logged-in user, aka the sender
$user->befriend($recipient);
}

The above form snippet you posted wont work because the form is not sending anything, do something along the lines of this

{{ Form::open(['method' => 'POST','route' => ['friendships.sent']] )}}
@csrf
<input type"hidden" name="recipient_id" value="2"> [or whatever is the ID of the user you want to be friends with]
<button class="btn btn-success" type="submit">Connect</button>
{{ Form::close() }}

Hope this helps.

antweny commented 5 years ago

Thank you very much for the support. Let me work on it if i face any problem i'll ask for support.

antweny commented 5 years ago

i want to get the list of all the users except those we are connected as friends andi have sent the request. how can i do this??.

TechTailor commented 5 years ago

I don't think such a function is build into the system. If you want to display the user a list with all of your other users (minus friends) for them to add, then that is a good source for spamming. It is advised that you befriend a user if and only if you know something about them (like email, username, full name, etc).

Can you tell me your use case for this? or what you are trying to achieve?

antweny commented 5 years ago

I want the system to be able to suggest users to add as friend except those who are their friends, request has been sent to/from, blocked user

antweny commented 5 years ago

I want the system to have ability suggest other users to add them as friend except those who are already their friends, request has been sent to/from, blocked user

antweny commented 5 years ago

This is what i want to achieve in a proper way the current one is working but not a good approach.

My Controller file class HomeController extends Controller { public function __construct() { $this->middleware(['auth', 'verified']);

My view file. `@foreach($users as $user) @php $auth_user = Auth::user(); $recipient = \App\User::findOrFail($user->id); @endphp @if(($auth_user->hasSentFriendRequestTo($recipient) || $auth_user->hasFriendRequestFrom($recipient) || $auth_user->isFriendWith($recipient) || $auth_user->hasBlocked($recipient) || $auth_user->isBlockedBy($recipient) ) == 1) @else

  • {{$user->name}}
    {{ Form::open(['method' => 'POST','route' => ['friendships.sent']] )}} @csrf {{ Form::close() }}
  • @endif @endforeach`

    I want the if statement in a View file to do it in the Controller

    TechTailor commented 5 years ago

    Okay, I get it, you want something like Facebook/Twitter has for suggesting users. Well I will have to get some free time to be able to create a PR that would do this out of the box with eager loading, but for now you are on the right path.

    The STATUS column in the friendships table is used for describing the TYPE of Friendship. When you Deny, Block or Accept, all it does is change the Status integer (PENDING = 0, ACCEPTED = 1, DENIED = 2, BLOCKED = 3), so there is no need to run multiple checks, all you need to check is if that relation exists in the friendships table.

    ### In your controller -
    -----------------------------------
    $users = User::all()->except(Auth::id()); // using except here to eliminate the logged in user from the collection
    
    return view('yourview')->with(compact('users');
    -----------------------------------
    ### In your view -
    -----------------------------------
    @foreach($users as $user)
     @if(! Auth::user()->getFriendship($user))
      {{ $user->name }}
     @endif
    @endforeach
    

    the getFriendship() method returns the pivot relation between the Auth::user() and the $user from the Users table. So we call this method for each user in our users table and then display the user details only when the relation does not (!) exists. Check if this works for you.

    Some suggestions -

    $auth_user = Auth::user();

    There;s no need to assign to another variable, you can use Auth::user() or auth()->user() directly.

    @foreach($users as $user) $recipient = \App\User::findOrFail($user->id);

    You already have the $user->id from foreach, no point in calling the User Model Again for $recipient

    -TT

    antweny commented 5 years ago

    Thank you very much it worked fine

    @foreach($users as $user)
     @if(! Auth::user()->getFriendship($user))
      {{ $user->name }}
     @endif
    @endforeach

    alse the reason why i didn't do this way $users = User::all()->except(Auth::id()); is because i want to get only those users who have already verified their accounts via e-mail thus why i'm doing this way $users = User::select('name', 'id')->where([['id','!=',Auth::user()->id],['email_verified_at','!=',NULL]])->get(); if there is a best way please share with me.

    TechTailor commented 5 years ago

    Glad that it worked. Close this issue if you are done.