diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php index 747dbb6..3a8d296 100644 --- a/app/Http/Controllers/FriendshipsController.php +++ b/app/Http/Controllers/FriendshipsController.php @@ -74,7 +74,7 @@ public function store(Request $request) })->exists(); if ($friendshipExists) { - return back()->with('error', 'You are already friends or a request is pending.'); + return back()->with('error', 'You are already friends, blocked or a request is pending.'); } Friendship::create([ @@ -115,17 +115,50 @@ public function destroy($friend) { $friendship = Friendship::where(function ($query) use ($friend) { $query->where('user_id', Auth::id()) - ->where('friend_id', $friend); + ->where('friend_id', $friend); })->orWhere(function ($query) use ($friend) { $query->where('user_id', $friend) - ->where('friend_id', Auth::id()); + ->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.'); - } + } + + public function block($friend) + { + $friend = User::findOrFail($friend); + + if ($friend->id === Auth::id()) { + return back()->with('error', 'You cannot block yourself.'); + } + + $friendship = Friendship::where(function ($query) use ($friend) { + $query->where('user_id', auth()->id()) + ->where('friend_id', $friend->id); + })->orWhere(function ($query) use ($friend) { + $query->where('user_id', $friend->id) + ->where('friend_id', auth()->id()); + })->first(); + + if ($friendship) { + if ($friendship->status === 'blocked') { + return back()->with('error', 'This user is already blocked.'); + } + $friendship->update(['status' => 'blocked']); + return back()->with('success', 'User blocked successfully!'); + } else { + // Create a new friendship with 'blocked' status if no existing relation is found + Friendship::create([ + 'user_id' => auth()->id(), + 'friend_id' => $friend->id, + 'status' => 'blocked', + ]); + return back()->with('success', 'User blocked successfully!'); + } + } } diff --git a/public/source/assets/images/block_icon.png b/public/source/assets/images/block_icon.png new file mode 100644 index 0000000..42d4d2e Binary files /dev/null and b/public/source/assets/images/block_icon.png differ diff --git a/resources/views/friendships/index.blade.php b/resources/views/friendships/index.blade.php index 034e872..a357504 100644 --- a/resources/views/friendships/index.blade.php +++ b/resources/views/friendships/index.blade.php @@ -38,6 +38,13 @@ + +
@endforeach diff --git a/routes/web.php b/routes/web.php index fb6db74..14edbe4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -28,7 +28,7 @@ Route::get('/friends/pending', [FriendshipsController::class, 'pending'])->name('friends.pending'); Route::post('/friends/{friendship_id}/accept', [FriendshipsController::class, 'accept'])->name('friends.accept'); Route::post('/friends/{friendship_id}/decline', [FriendshipsController::class, 'decline'])->name('friends.decline'); - + Route::post('/friends/{friend}/block', [FriendshipsController::class, 'block'])->name('friends.block'); });