This episode is for members only

Sign up to access "Build a Friend System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
11. Unfriending friends

Transcript

00:00
Removing a friend is going to be pretty straightforward because we already have a controller to deal with this. So let's just go ahead and hook this up over on the friends index page. We're going to return back to the profile page as well a little bit later to look at the logic around this.
00:14
Okay, so to unfriend someone, it's just going to be the same as cancelling or rejecting a request. So we can pretty much just grab this entire form. Like I said earlier, you could create separate controllers out for these if you wanted specific things to happen when these actions had happened, but we're just going to share them for now.
00:33
Okay, let's go ahead and paste this form into here. Let's just make sure this goes through to friend destroy but actually calls the friend itself because we're now within the friends list and we want to go ahead and delete but we want to change this over to unfriend. It's pretty much all we need to do.
00:49
So if I want to go ahead and unfriend Tabby, I can do that. And of course from Mabel's side as well. She could unfriend me and that also deletes the relationship. So now no one has any friends.

Episode summary

In this episode, we're adding the ability for users to unfriend each other on our app! The process is pretty easy because we already have most of the backend logic in place thanks to our existing controller.

First, we jump to the friends index page and hook up the 'unfriend' feature. You'll see that unfriending someone works very similarly to cancelling or rejecting a friend request. We're even reusing the same form for both actions, keeping things simple and DRY (don't repeat yourself). While you could separate these with different controllers for more custom behavior, we're opting for the streamlined route to keep our codebase cleaner.

After hooking everything up, we test it out: if you want to unfriend someone like Tabby, it's just a click away. And as a bonus, the person you unfriend can do the same to you, immediately updating both users' friend lists. By the end of this video, you'll have a fully working unfriend feature—no one is stuck being friends if they don't want to be!

Episode discussion

No comments, yet. Be the first!