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
13. Preventing adding self as friend

Transcript

00:00
Before we move on to some other more fun stuff like looking at the statuses of our friends. Let's look at some more logic Particularly around adding ourself as a friend because at the moment I can technically add myself as a friend Which we don't really want to happen. It doesn't make any sense at all
00:17
So let's figure this out. Now. The first thing that we'll do is look at the controller itself So if we come over to friend store controller We have a check-in here at the moment to see if we have a pending request already But this doesn't check if the user is adding themselves as a friend. So this is pretty straightforward
00:34
all we really need to do in here is just compare the current user that we've got and The ID that would probably make more sense and check if it Equals the users ID that we're trying to add as a friend if that's the case We can just return back
00:49
so a really simple check just to see if we're trying to add ourselves as a friend and of course if we now click on That we're redirected back and we don't add ourself as a friend You could add this at the model level if you wanted to so you could extract this into a method over on your user model But I tend to find this bloats things out and also doing these kind of checks directly within your controller
01:13
Give you the opportunity to flash a message to tell the user why the request didn't work Okay, so the next thing is going ahead and making sure that we remove this button if we're viewing our own profile So let's come over to our profile page here And of course, we don't want to see any of this in here if we are that user
01:33
So again, we're gonna nest this a little bit too much, but that's absolutely fine We can always clear that up later and we're just gonna go ahead and check if the currently authenticated users ID doesn't equal the
01:46
User ID of the profile that we're viewing and of course, let's go down and end that if This is way too many nests, but that's absolutely fine for now And let's just go up and indent all of this and we should be good So, of course now we don't see that but if we head over to someone else's profile
02:04
We see that in there now one other thing that we want to do is make sure that if we already have a friend Like Mabel that we can't add them again as a friend. So if we just look at This in here, we've got user ID of two has added user ID of one regardless of this relationship
02:25
Direction whether one has added two or two has added one. We don't this to be added again now for this We want to do this over in the controller. What I'm actually going to do is go over to the profile index page I'm gonna grab the add as friend form and just temporarily Put this at the top here so we can test this is working
02:46
Let's pull that in let's go over and I'm gonna try and add Mabel as a friend again now It looks like nothing's happened. But actually this has created a duplicate in here, which we don't want now that we have that friend relationship What we can do is we can check if the friend already exists and we can just return back again So once again over on the friend store controller, we can create another if statement in here to reject this if that's the case
03:10
So we're just gonna say if the user friends doesn't matter which direction we do this in whether it's the Authenticated users friends or the current users friends contains and I think it would actually make a bit more sense To do it this way round contains user
03:27
Then we want to go ahead and return back again. And again, we could flash a message in here Not that the user should see that anyway, so if we come over now and hit add as a friend We see that that's not duplicated and we can do this the other way around as well So we're currently signed in as Alex
03:44
but if we sign in as Mabel and try and add Alex again notice that we still don't get that duplicate because this is looking At the relationship in both directions. Okay, so we've got three if statements in here. These could probably be Moved out to middleware or something like that. I'll let you decide how best you want to structure this Let's just get rid of this form inside of here and we are pretty much done with all of our friend logic
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!