Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get profile model data when getting all pending friend requests for profiles. #79

Open
YosefOberlander opened this issue Jan 27, 2022 · 6 comments

Comments

@YosefOberlander
Copy link

have a profile model with the users information and I’m importing Friendable. I’m able to send and receive friend requests successfully but when I call ‘auth()->user()->getCurrentUserProfile()->getFriendRequests(), I only get the ‘friendships’ table data, not the profiles. I’d only get profiles data once the user accepts the friend request. I would like to get the profile data when calling ‘auth()->user()->getCurrentUserProfile()->getFriendRequests() so I can display a users profile to a user who receives their friend request.

@PonyUpDaddy
Copy link

@YosefOberlander did you find a solution for this?

@YosefOberlander
Copy link
Author

@PonyUpDaddy I ended up making my own friendship implantation.

@ronssij
Copy link

ronssij commented Oct 19, 2022

@YosefOberlander
I think the better way for it is to make new Model that extends the Friendship model.

Then add this:

/**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function friendRequests()
    {
        $friendshipModelName = Interaction::getFriendshipModelName();
        return $this->morphMany($friendshipModelName, 'recipient');
    }

and then you can do this.

auth()->user()
 ->getCurrentUserProfile()
 ->friendRequests()
 ->whereStatus(Status::PENDING)
 ->get()

or you can make you own model that extends the Interaction model so you can add the relation to the user model and you can eager load te re friendRequests() relation like.

User::query()
   ->getCurrentUserProfile()
   ->with('friendRequests.user')
   ->whereStatus(Status::PENDING)
   ->get()

or if not you can make your own relatinship to user model using the iteraction table with hasManyThrough()

@prawn185
Copy link
Contributor

prawn185 commented Feb 17, 2023

I did something like, I think it's the same issue:

  $friendRequests = $friendRequests->map(function ($friendRequest) {
            $friendRequest->user = User::find($friendRequest->sender_id);
            return $friendRequest;
        });
//returns User model 

@ronssij
Copy link

ronssij commented Mar 24, 2023

@prawn185
Oh! That can introdice an N+1 query. It should be on the sql layer imoo.

@craigand
Copy link

craigand commented Apr 6, 2023

@YosefOberlander I think the better way for it is to make new Model that extends the Friendship model.

Then add this:

/**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function friendRequests()
    {
        $friendshipModelName = Interaction::getFriendshipModelName();
        return $this->morphMany($friendshipModelName, 'recipient');
    }

and then you can do this.

auth()->user()
 ->getCurrentUserProfile()
 ->friendRequests()
 ->whereStatus(Status::PENDING)
 ->get()

or you can make you own model that extends the Interaction model so you can add the relation to the user model and you can eager load te re friendRequests() relation like.

User::query()
   ->getCurrentUserProfile()
   ->with('friendRequests.user')
   ->whereStatus(Status::PENDING)
   ->get()

or if not you can make your own relatinship to user model using the iteraction table with hasManyThrough()

In case anyone gets stuck implementing this solution like I did, you need to update the acquaintances.php config file friendship model under models->friendship for the extended model to work.

My code to fetch the pending friend requests I ended up using was this:

$user = Auth::user();

$friendshipModelName = Interaction::getFriendshipModelName();

$friendRequestsReceived = $friendshipModelName::whereStatus(Status::PENDING)->whereRecipient($user)->with("friendRequests", "sender")->get();

Edit: changed code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants