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
07. Avoiding duplicate requests

Transcript

00:00
I'm going to go ahead and remove the record that we've already got for that friend request. And now we're going to go ahead and create a check here to see if we already have a pending request
00:09
for a particular user. We're going to do that over on the user model. So let's do this just at the top here before all of our relationships. So let's create a method out called has pending friend request for, and then we can accept in a user to see if we have a pending request for that particular user. So how are we going to do this? Well, we can go ahead and access the
00:31
relationship here. So pending friend two, and then we can see in this collection if it contains that user. Very elegant within Laravel to do that. So we can now take this method name here and over in the friend store controller, we can create a check to see if that's the case. So we're going to go ahead and grab the currently authenticated user and see if they have a pending
00:55
friend request for that user already. If they already do, then we're just going to return, if I can spell return, back to the other page. So now when we go ahead and add this user as a friend, that gets added. But when we do it a second time, it doesn't work. Now what we can do is actually use this method to change around the UI as well. So let's go over to the profile index page and
01:19
focus on this. So let's just space this out a little bit. We've got our form here already. The first thing that we're actually going to do is wrap everything that we do here in an auth check. So let's end the auth down here. Because, of course, we don't want any of these actions to show if the user is a guest. So now what we can do is add an if statement in here to say if the
01:44
currently authenticated user already has a pending request for that user, then we want to show a different form, maybe to cancel the friend request or do something else. So let's say in here, waiting for, and let's give the user's name, to accept your friend request. That should just be enough for now to see that that's working. And there we go. So we've already added that user.
02:14
And, of course, we know that we're waiting. So I'm going to get rid of that and come back. I'm going to click on that. Sure enough, it switches over really nicely. So we also want the ability to cancel this. So I'm just going to grab this form here. We're not going to do that quite yet. But we're going to put this in here and have the ability to cancel a friend request. I'm going to
02:32
get rid of the action in here for now. And let's just look at the spacing of this, because at the moment, it's just hovering down the bottom. Let's just go ahead and wrap this entire thing in a div. And let's just go ahead and indent this. We'll put this in a span. And what that means is we can now just space this out nicely. So let's say space x maybe 2. And let's go ahead and just make this
02:54
form inline. Of course, this doesn't really matter. And there we go. So let's just bump this down to 1, actually. Yeah, that's OK. So we can click this now to cancel the friend request. So now is probably a good time to move on to looking at canceling a friend request as well. We're also going to go ahead and add these to our friends page just over here.
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!