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
09. Accepting friend requests

Transcript

00:00
So we have the ability to reject a friend request. Now we're going to look at accepting a friend request. And then we're going to move on to actually merging all of our friends in.
00:09
So on both sides, both Alex and Mabel, we can see that list of friends. We'll get to that in the next episode. But for now, we're looking at accepting. So again, we're going to go through the same,
00:20
pretty much the same process of just creating our controller that modifies this pivot table. So let's start out with a controller here. And we'll keep to convention. And we'll go ahead and call this friend patch controller.
00:33
So we're modifying part of this. And let's head over to routes and web. And let's just copy this down. Call this patch.
00:42
Again, we're still passing a user in. And we'll call this friend patch. OK. So let's go and grab this name, hook this up, and send a request through.
00:53
So over in our friends just here, we're going to go down to where we have accept. And we'll just paste this in here. So we've got it. Copy this.
01:05
Paste this in here. And we can send a patch request. So method spoofing here is going to work in exactly the same way. Friends.patch.
01:14
And that should be everything we need. We just need to say accept here. So if we come over, at the moment, we have an invalid route action, which is absolutely fine. So let's come over to our friend patch controller.
01:25
Once again, we need to be authenticated here. So we'll just add in our middleware directly in our controller. And under invoke, we need to know the user that we're updating. And we'll bring in our request so we can grab the currently authenticated user.
01:39
OK. So once again, let's just die dump on that user. Let's come over. And we'll go over to Mabel's account specifically and accept this request.
01:50
There we go. So now what we want to do is update that existing pivot within here and set this to true or one. So to do this, we're going to go ahead and grab the currently authenticated user. Access the pending friends from.
02:06
Because remember, we're accessing and accepting from the pending friends that we are receiving. And we're going to update an existing pivot here. For that particular user that we're adding. So if this was Alex sending a request to Mabel, I would be updating the record where,
02:28
of course, user ID is one and friend ID is two. And then as a second argument to updating an existing pivot, we just pass in all of the pivot items that we want to update. And in our case, accepted is going to be set to true.
02:42
And then we can just go ahead and return back. So let's try this out. Let's go over to Mabel's account, accept. And there we go.
02:51
Or we have no from requests. And if we come over to Alex's account, we have no pending from requests anymore. Because, of course, this has now been accepted. So now is the question, how do we get a list of our friends?
03:05
Well, that's a little bit more complicated because we have a two way relationship here that works by the friends that we've added and the friend requests that we've received. So let's head over to the next episode and see how we can figure out grabbing a list of all of our friends, regardless of who has added who.
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!