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
05. The friend relationships

Transcript

00:00
So that is quite enough scaffolding. We're now going to get down to the actual friends table and the relationships to kind of make this work.
00:08
We're going to need to do a little bit more work to sort of merge the user's friends in, but we will get to that. So the first thing is, of course, the table.
00:16
So let's go ahead and create out a migration here. We're not going to use a model to represent the friend, so we're not going to make a model for this. But we're going to create that friends table,
00:26
which is essentially a belongs to many table that hooks two users up. So let's create a friends table out here and let's fill this in.
00:35
So let's head over to create friends table and let's go down and see what we need to do. So of course, we're going to have two people here, the user and the friend.
00:45
So we're going to start off with a foreign ID and that's going to be the user ID. This will be the person who is adding the friend. So if I was adding Mabel, this would be ID of one.
00:58
And then we're going to have another foreign ID in here, which is the friend ID. So in our case, that would be user ID of two. So Alex has added Mabel.
01:08
Now I've constrained these for foreign key integrity, so they have to exist in the database. But this is also helpful because if we use friend ID, we don't have a friend table.
01:19
So what we want to do is hook this and link this back to the users table. We can't have two user IDs because, of course, columns are unique within a database,
01:29
but friend ID exists over on the users table. So that's pretty much what we need to do in terms of them two users adding each other or this user adding this user.
01:39
And we'll look later at how we can see the kind of reverse relationship of this. Now there's one more essential thing that we need here and that is whether this friend request
01:50
has been accepted or not. Now for this, I'm not going to go ahead and use a Boolean because a little bit later on, specifically with Postgres,
01:57
we're going to see an issue with this. You can use a Boolean, I think, if you're using MySQL, but I'm going to go ahead and use an integer in here to denote whether this is accepted or not.
02:07
And that will be a zero if it hasn't been. That will be the default and one if it has been. So I'm going to go ahead and actually add a default at the database level here to zero.
02:18
So by default, of course, a friend request is not accepted. Okay, that's pretty much all we need to do. So let's go ahead and run phpArts and migrate. And we're going to head straight over to the user model
02:29
to hook up these relationships. Now we have two different ways of defining these relationships. So if Alex adds Mabel,
02:38
then Alex is going to be user ID and Mabel is going to be friend ID. But when we try and grab this relationship for Mabel, that's not going to work
02:46
because she is friend ID and not user ID. So we also want a way to say that Mabel is friends with Alex or at least has a friend request from Alex.
02:56
So we're going to need to define two different relationship types and then later merge them. Now we're not going to be merging the collections together.
03:05
We're going to be using a specific package to merge these together using a database view. And we'll look at that a little bit later. So naming these relationships is a little bit tricky.
03:16
What I'm actually going to call this is friends two. So let's just define the method names out first of all. And let's create another one here called friends from. So for example, if Alex adds Mabel,
03:30
this relationship will return Mabel because it's the friends that we've added to. And Mabel will have a record in friends from because she has been added from someone else.
03:41
Hopefully that makes sense. But feel free to tweak these method names if you think a different name would make sense. So like I said, we have that pivot table in here.
03:50
So this is going to be a belongs to many relationship. And of course, this relates to a user because users add users. Now for this, we need to define in a few extra parameters.
04:01
First of all, the actual table name itself. And then we need to say user ID, which comes first in this case because friends two will be user ID of one
04:10
if it was me adding Mabel. And then we would have the friend ID in here. Now we've got that extra pivot in here as well. This accepted, which we're going to need to read from,
04:20
use where clauses for. So we're going to go ahead and use with pivot here and pass in that pivot. Okay, so for the friends from,
04:29
it's going to be pretty much exactly the same thing, but the opposite way around. So friend ID and user ID. That's pretty much all we need to do
04:37
to get them relationships working both ways. What we're now going to do is just add a couple of extra methods so we can get the pending friends to,
04:46
the pending friends from, and the accepted friends to, and the accepted friends from. Now, the reason that we need these
04:54
is because friends from doesn't care about this accepted pivot. It pulls it in for us, but it doesn't really tell us anything that's happening.
05:02
If we just remind ourselves over on our friends page, we're going to need all of our friends, which is a special relationship we're going to look at later.
05:09
But our friend requests are going to be pending request to. Oh no, our friend requests are going to be pending requests from, so who's added us,
05:19
and pending and friend requests is going to be pending friends to. So let's go down and define both of these. We can basically just piggyback off the relationships
05:29
that we've already created. So let's go ahead and start with pending friends to, and that's going to return this friends to, so we can use the same relationship,
05:41
but then we're going to add a where clause into here. So we're going to say where pivot accepted is false. So these are the people that we've added that haven't yet accepted us,
05:53
and we can do the same thing here, pending friends from. These are the people that are adding us that haven't quite been accepted.
06:01
So friends from, not friend friend. Now to be able to pull back a list of our friends, we're going to need to merge the accepted friends to and accepted friends from.
06:12
So let's create these two relationships out here, accepted friends to, and accepted friends from, and basically these two are just going to be
06:24
where accepted is true, or you could put one in there if you have to find a boolean. And that's pretty much it. We've now got the ways that we can grab
06:32
the pending friend requests, the accepted friend requests, and if we need them, the base relationships in here.
06:38
So now that we've got these relationships, we can actually start to test adding a friend, and then seeing our pending friend requests from the user that's adding,
06:47
and our friend requests from the person who's receiving the request. So let's go over to the next episode and focus on that.
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!