Endpoint
Authentication
This endpoint requires authentication. The user must be logged in to access this endpoint.Path parameters
The ID of the user to interact with
Behavior based on friendship status
The endpoint performs different actions based on the current relationship state:1. Accept incoming pending request
Condition: The target user has sent a pending friend request to the authenticated user. Action: Accepts the friend request by:- Updating the request status from
pendingtoaccepted - Setting the
response_attimestamp to the current time
2. Remove existing friendship
Condition: The users are already friends (accepted friend request exists in either direction). Action: Removes the friendship by:- Detaching the friend relationship from the
friend_requestspivot table - Works for both directions (whether the authenticated user sent or received the original request)
3. Cancel outgoing pending request
Condition: The authenticated user has already sent a pending friend request to the target user. Action: Cancels the friend request by:- Detaching the pending request from the
friend_requestspivot table
4. Send new friend request
Condition: None of the above conditions are met (no existing relationship). Action: Creates a new friend request by:- Creating a new entry in the
friend_requestspivot table - Setting
statustopending - Setting
send_attimestamp to the current time
Response
All actions return the same response structure with a success message.Success message indicating the action was completed. Always returns
friend_request_send regardless of which action was performed.State machine diagram
Examples
Send a new friend request
Accept an incoming friend request
Cancel an outgoing friend request
Remove an existing friendship
Implementation details
The endpoint uses Laravel’s Eloquent relationships to manage the friend requests:friendsS(): Friend requests sent by the authenticated user (sender)friendsR(): Friend requests received by the authenticated user (receiver)
friend_requests pivot table includes:
sender_id: ID of the user who sent the requestrecived_id: ID of the user who received the requeststatus: Eitherpendingoracceptedsend_at: Timestamp when the request was sentresponse_at: Timestamp when the request was accepted (null for pending requests)