laravel-acquaintances icon indicating copy to clipboard operation
laravel-acquaintances copied to clipboard

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

Open YosefOberlander opened this issue 2 years ago • 6 comments

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.

YosefOberlander avatar Jan 27 '22 00:01 YosefOberlander

@YosefOberlander did you find a solution for this?

PonyUpDaddy avatar Mar 24 '22 21:03 PonyUpDaddy

@PonyUpDaddy I ended up making my own friendship implantation.

YosefOberlander avatar Mar 28 '22 22:03 YosefOberlander

@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()

ronssij avatar Oct 19 '22 04:10 ronssij

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 

prawn185 avatar Feb 17 '23 14:02 prawn185

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

ronssij avatar Mar 24 '23 14:03 ronssij

@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

craigand avatar Apr 06 '23 01:04 craigand