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
08. Cancelling a friend request

Transcript

00:00
So let's go ahead and look at cancelling a friend request. And as part of this, we'll also go ahead and update
00:04
our friend request and our pending friend request sections so we can see these from both of the user's sides. So I already have a browser tab open here in Incognito, which I'm going to use to log in as Mabel
00:17
to see that request come through the other side as well. Let's first of all just focus, though, on cancelling this request. And then we can hook this up both on the profile page
00:26
and on our friends page. So to cancel a friend request, this is going to be the same thing as unfriending. It's essentially just going to delete that record
00:35
in the pivot table. So let's go ahead and create out a controller for this. And we're just going to call this Friend Destroy Controller. And let's head back over to that.
00:47
So Friend Destroy Controller. Once again, we need to be authenticated for this. So let's add in some middleware. Of course, we could do that at the root level as well.
00:58
It might be a little bit easier. And let's go ahead and add in our invoke magic method. And we're going to be able to pass a user into here to get rid of that user.
01:08
So let's just make sure we pull the namespace in for that. Okay, again, let's just die dump on that user model just to make sure we know it's working. And let's head over to our web routes
01:16
and go ahead and create this out. So this is going to be a delete request through to friends with that user. We're going to use the Friend Destroy Controller,
01:24
and we're going to name this Friends Destroy. So we can pretty much head over to the profile index page and hook this up now. So we know the root name is Friends and Destroy.
01:38
And we can pass that user in. Now for this, it's not quite going to work because if we just take a look here and hit cancel, you can see that nothing really happens.
01:48
That's just because we're posting through to this and it's not actually sending a delete request. For this, we use method spoofing. So we can use the method directive
01:57
and we can pass in the actual method that we want to spoof. So if we come over now and click cancel, you can see sure enough, this now goes through and dumps that user we want to delete.
02:07
Now, if we come over to the database table, we've still got this in here. This will remove this regardless of whether it's accepted or not,
02:13
which means we can reuse this controller to also unfriend someone as well. So the question is, how do we delete this record? Well, we just go ahead and detach it.
02:23
So we're going to go ahead and say request and user, and we want to use the base friends from relationship in here or friends two in this case. We need to think about this from both sides as well.
02:35
So we'll be adding another little method in here as well. And we basically just want to detach this from here. So that's going to remove that user from here. So let's say we passed in user two,
02:46
that's going to remove that from this relationship from user one. So we'll just basically remove that and then we can return back.
02:54
So we can test this now by hitting cancel, and you can see sure enough that record has been deleted. Now, we need to think about this really, really carefully because if we were to cancel the friend request
03:03
or reject the friend request from Mabel's side, Mabel doesn't have any friends under this friends two relationship. Instead, she has them under friends from,
03:13
because remember, this two-way relationship works with the user ID being the person that added and the friend being the person who is being added. So that's where our friends page over here comes in,
03:27
which we can now use to update our friend requests and our pending friend requests. So let's do that and then we'll come back to this and see how we need to update this.
03:36
Okay, so let's go over to our friend index controller and think about what we want to pass down to this view. We'll go ahead and bring our request into here so we can extract this information out.
03:47
And we want to go ahead and output the pending friends to, so that's going to be request user pending friends to, and the same for pending friends from, pending friends from.
04:01
So we can now use this to iterate through them users, add the appropriate buttons and test that this works. So over on our friends index page, let's go and start to iterate through this.
04:14
So we've got our friend requests here, which remember is pending friends from, and down here, our pending friend requests, which is people we are trying to add.
04:25
Let's start with pending friends to, because we've already added Mabel as a friend. So let's go ahead and iterate through this to output these. So let's do a for each here on pending friends to as,
04:38
and let's just call this pending friend. And let's end that if statement down here. Let's go over and have a look. And of course that should be end for each.
04:51
And if we come over, now we don't have any pending requests at the moment. So let's go over to profile and to, and add Mabel. And then if we come back over, there she is.
05:03
Of course, we're not outputting that user's name at the moment, but we can very easily output pending friend, and get rid of that extra dollar.
05:15
And just say, name. And there we go. Mabel is now pending under our friend request. Now we can cancel this.
05:22
So we can do pretty much what we did over on the profile index page here. So we can grab this form, and essentially just pop that into there.
05:31
Let's do that and make sure we just pull this in. And we want to go to friends destroy user is now pending friend. And that should be everything we need.
05:43
So if I click cancel, it's going to work in exactly the same way. And it's going to cancel that friend request much like we saw over on the profile page,
05:51
which I'm actually going to bring up here. So we have it as a reference. So add as a friend, and there she is. So let's just, first of all, hook this user up here,
06:01
or hook this up. And I'm just going to go ahead and indent this form here. So we can hook this up to the profile page. So at least we can click through.
06:09
So profile, and that is our pending friend. There we go. So we can click through to that profile now. And let's now fill in our from request,
06:20
then go over to Mabel's account and see this in here. Of course, if we go over to Mabel's account now, let's sign in with Mabel's account here. And we go over to our friends here.
06:31
We don't have any pending from requests, but we should have a friend request coming through. Now, just before we go on, let's actually use a for else here.
06:41
So let's say end for else. And what we can now do is say empty and say you have no pending friend requests. And in this case, we don't see that,
06:54
but in Mabel's case, we now just see a little note there. Okay, so we can pretty much do exactly the same thing here with this for else for our friend requests. So let's just pop that in there.
07:07
Let's go ahead and add in our empty and end for else. And just saying here, you have no friend requests. And in here, we can go ahead and iterate through our pending friends from,
07:23
and let's say pending friend from, and just change this up because we want this to kind of make sense. So pending friend to,
07:31
and we'll add that into there as well. And that makes a little bit more sense, but of course you can change this around. So this friend here is gonna be pending friend from name,
07:42
and then we'll fill in the accept and reject when we get to them. Reject we can do because we can just delete that, but let's come over and yeah, pending friend,
07:51
let's just see where we didn't change this up. So that would be there. And there we go. So you have no friend requests,
08:00
but of course, if we head over to Mabel, I am trying to add Mabel and we can accept or reject this request. So we don't have the ability to accept a friend request yet,
08:11
but we do have the ability to reject a friend request. So if we just fill in the form for this, which is essentially the same as cancel, you could technically create different controllers for this.
08:24
Maybe if you wanted to email the user to tell them that their friend request had been rejected. At the moment, deleting a friend and rejecting or canceling a friend request
08:33
are all the same thing. So you could just create different controllers for these and then that would give you a little bit more flexibility. So let's go ahead and paste that form in here.
08:42
Let's change the pending friend to pending friend from, and that should be pretty much everything we need. So this is gonna be reject. Okay, let's come over to Mabel's account and hit reject.
08:56
So now what we're doing is going back to what we spoke about earlier. When I hit reject on this, it doesn't quite work. However, if I'm on Alex's account
09:05
and I cancel this, it does work even though it's using the same controller. So let's just go over and re-add Mabel as a friend, come back over to her account,
09:13
and just have a look at why this isn't working once again. So over in our code, if we come over to friend destroy controller, remember this is using the friends to relationship.
09:23
Now in terms of Mabel, if we have this request in here, Mabel is user ID of two. The friends to relationship relies on the user that's performing the action to be the user ID.
09:35
That means that Mabel doesn't have any records in that particular relationship. So what we need to do is we either need to just duplicate this down and say friends from detached,
09:46
so we do both, or we could just change this up a little bit with an if statement so we're not executing two queries. Let's run this first of all just to make sure it works. So if I hit cancel here, you can see that works.
09:58
I'm gonna add Mabel back as a friend and come back over to reject and you can see that that also works. So we're basically always making sure we do this
10:06
from both sides, but it's not as efficient because we are always performing two queries here. So a better way to do this would be to take one of these, so for example, friends to,
10:18
and put this inside of an if statement. So we know that if that was successful, then we can go ahead and return back and not execute this second query.
10:28
Let's just get rid of that semicolon and that will pretty much always run one query regardless of who is sending this request through. So let's just try this out.
10:38
If we just come over here and add Mabel as a friend and cancel it, that still works. If we go over to Mabel's account and we reject this friend request, that also works,
10:46
but we're always just running one query here. Okay, so I'm gonna go over and re-add Mabel as a friend and next up, we're gonna look, of course, at accepting friend requests.
14 episodes1 hr 16 mins

Overview

Everything you need to implement a friend system in Laravel, built in a simple UI so you can integrate it into your existing apps.

We'll start with the basic friend relationships, then add in more advanced relations to make things future-proof. Want to easily get all friend's statuses on a timeline? No problem.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!