From 66f88977e155330d0aad432249dcb53cb8ff70d9 Mon Sep 17 00:00:00 2001 From: Jaton David Date: Thu, 7 Nov 2024 10:24:57 +0100 Subject: [PATCH] Delete a user --- .../Controllers/FriendshipsController.php | 30 ++++++++++++++----- resources/views/friendships/index.blade.php | 18 +++++++---- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php index 3217d2f..747dbb6 100644 --- a/app/Http/Controllers/FriendshipsController.php +++ b/app/Http/Controllers/FriendshipsController.php @@ -37,14 +37,14 @@ public function create() public function pending() { $pendingRequestsFromFriends = Friendship::where('friend_id', Auth::id()) - ->where('status', 'pending') - ->with('user', 'friend') - ->get(); + ->where('status', 'pending') + ->with('user', 'friend') + ->get(); $pendingRequestsFromUser = Friendship::where('user_id', Auth::id()) - ->where('status', 'pending') - ->with(['user', 'friend']) - ->get(); + ->where('status', 'pending') + ->with(['user', 'friend']) + ->get(); $pendingRequests = $pendingRequestsFromFriends->merge($pendingRequestsFromUser); @@ -111,7 +111,21 @@ public function decline(Request $request, $friendship_id) return back()->with('error', 'Could not decline the friend request.'); } - public function removeFriend(Request $request) + public function destroy($friend) { - } + $friendship = Friendship::where(function ($query) use ($friend) { + $query->where('user_id', Auth::id()) + ->where('friend_id', $friend); + })->orWhere(function ($query) use ($friend) { + $query->where('user_id', $friend) + ->where('friend_id', Auth::id()); + })->first(); + + if ($friendship) { + $friendship->delete(); + return back()->with('success', 'Friend removed successfully!'); + } + + return back()->with('error', 'Could not find the friend to remove.'); + } } diff --git a/resources/views/friendships/index.blade.php b/resources/views/friendships/index.blade.php index 5169b58..034e872 100644 --- a/resources/views/friendships/index.blade.php +++ b/resources/views/friendships/index.blade.php @@ -26,12 +26,18 @@
- - + + + +
+ @csrf + @method('DELETE') + +
@endforeach