laravel-acquaintances
laravel-acquaintances copied to clipboard
How to get profile model data when getting all pending friend requests for profiles.
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 did you find a solution for this?
@PonyUpDaddy I ended up making my own friendship implantation.
@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()
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 Oh! That can introdice an N+1 query. It should be on the sql layer imoo.
@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