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
06. Adding friends

Transcript

00:00
We're now going to go ahead and add Mabel as a friend. And we're going to make this really, really simple
00:04
without any additional checks. We'll focus on them in the next episode. But let's start out, of course, with a controller just to get this working and see how we get on.
00:12
So we're going to go ahead and make a controller in here. And we're going to call this Friend Store Controller. So this will be to create a friend request. So if we head over to that Friend Store Controller,
00:23
really importantly, we need to be authenticated for this. So let's start out with our middleware. So let's define out our standard auth middleware in Laravel in here.
00:31
And that should be just enough for now. Now, inside of Invoke, we're going to get a user into here with root model binding. So let's just define that in here now.
00:40
And let's also put in our request information so we can grab the currently authenticated user. Let's just put in the user model here. And for now, let's just die dump on that user.
00:51
OK, so over to our web root, let's go ahead and just copy one of these down. And this is going to be a post request through to friends. Let's actually organize these a little bit better
01:02
and put these next to each other. OK, so this is going to be Friend Store Controller. And we can just get rid of the name for this. In fact, let's call this Friend Store to keep things simple.
01:16
And that's pretty much it. So if we head over to the Profile Index page, we can go ahead and hook this up in our form now. So let's say root and Friends Store.
01:31
And because we're passing a user into there, let's pass that entire user in. We didn't actually define that here, so let's do that now. And that should be enough.
01:41
So we should be able to click this button and see that user dumped out. Let's click, and there we go. Now, the reason that we're doing this as a form and not as a link
01:49
is that we really want to use cross-site request forgery protection here, or someone else could add a user on your behalf, which is really not a good idea. OK, so we've got this working.
02:00
Now let's create the database record for this. So over in Friend Store Controller, we want to go ahead and grab the currently authenticated user, like so.
02:10
We want to go ahead and access the pending Friends 2 relationship. Remember, we're adding someone here. And we very simply want to attach that user.
02:21
And then we can just return back. That's pretty much it. So let's go over, hit this, and that user has now been added. Of course, we don't see anything here,
02:30
because we don't have any kind of if statements around here. But sure enough, you can see here that we have got this user, Alex, adding Mabel. Of course, accepted is zero by default.
02:40
Now, it might actually be useful to include the timestamps in here. So let's go ahead and do that now. So if we just head over to our user model,
02:49
let's go to our base Friends 2 and Friends From. And let's say with timestamps. It's pretty important. And we'll do the same for this one as well.
03:01
So let's say with timestamps. And there we go. So now, when we add that user in, we can see, or at least fetch, when they have been added.
03:11
OK, so now that we've actually created this friend request, we want to do a couple of things. We want to update the UI to show that this has been added.
03:20
And we're waiting for Mabel to accept our friend request. And we also want to add some additional checks over on the Friend Store Controller. Really importantly, if we just keep adding Mabel as a friend,
03:31
at the moment, at least, this is going to go ahead and keep creating records. Now, we could do this in a couple of ways. We could create our attach slightly differently.
03:42
But we're going to go ahead and add in a very specific check here with an if statement in the next episode to prevent this. That also gives us the opportunity to flash a message to tell the user
03:52
that they've already added Mabel as a friend and that we're waiting for her to accept. So let's head over to the next episode, get this cleared up, and see what we can do.
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!