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
10. Getting all friends

Transcript

00:00
Now, we want to focus on, of course, a really important part of this, and that's actually getting a list of our friends.
00:05
We've already spoken about how this isn't quite going to work because we have two different relationships over on the user model. We've got our friends to, which are friends that we have added, and friends from. Now, if we were to just pick one of these relationships, that would only give us the
00:22
friends that we have added or the friends that we have received. So let's go ahead and just pass one of these through to our template, see where the problem lies, and then we're going to look at a package that we can use to resolve this using SQL Views.
00:39
So let's go over to our friend index controller, and let's just get a list of our friends. And we'll just pick any of these relationships. Probably best to grab the accepted friends, of course, because we don't want to list pending friends under our friends list.
00:55
So let's go ahead and say user accepted friends to. So these are just going to be the users that we've added. Now if we head over to profile or friends and index, let's go ahead and list through our friends.
01:08
So this is just up here. So we're going to go ahead and say for each, and we can actually use a for else here. else as friend, and let's go down here and include our empty part, and end that for else. So let's say you have no friends.
01:32
OK, so in here, we'll output the friend's name, and then we'll leave this to a little bit later. So if I give this a refresh here, Alex does have one friend, which is Mabel. And if we head over to Mabel, this friend request has been accepted, but we don't have
01:51
any friends. Now hopefully by now it makes sense why this isn't quite working. If we were to switch this relationship that we're using over our friend index controller to friends from, that would reverse it, and Alex would have no friends, but Mabel would
02:05
have Alex as a friend. So what we need to do is work out a way that we can merge these two things together to grab a list of our friends, regardless of who has added who. Now one way to do that, if we head over to our user model here, would be to create a
02:19
merged collection. Now this wouldn't be ideal, because we wouldn't be able to paginate this. We wouldn't have any of the standard methods that we'd be able to use. Ideally we want an actual relationship here.
02:32
Let's just take a really quick look at merging a collection, just in case it was suitable for you. And we could just call this friends, so we could just have a friends method in here. And we could say something like this accepted friends to, and we could merge this accepted
02:52
friends from. So we're not going to call the method itself, we're going to grab both of the collections and merge these in. So if we now go over to our friend index controller, and we call that friends method, and we come
03:04
over to Alex's account, Mabel is a friend, and if we come over to here, Alex is a friend. So that does work, and if that was pretty much all you needed to do here, then that would work perfectly. The downside is eager loading is going to be really tricky, and for example, later on
03:21
in the course we're going to look at getting related models through friends, so we're going to get a list of all of our friend statuses. This isn't going to quite work. So let's go and just get rid of this, pass in an empty collection so it doesn't break,
03:37
and then let's go and look at how we can properly do this using the Laravel merged relations package. So essentially what this will do is it will do pretty much what we've already done over on our user relationship.
03:50
It won't actually merge two collections, but it will have the same effect, but this will be done at the database level, so it will still act like a relationship, and it will do this by creating out an SQL view which merges these two things together. So we're actually going to go ahead and get rid of this friends relationship because that's
04:06
not quite going to work, and we're going to define the actual relationship down here once we've got this package installed and our SQL view created. So let's go all the way down here, and let's install this first of all, and I'll walk you through on how we're going to do this.
04:22
Okay, so that's installed. Let's go and head over to our view section here. What this will do is using the schema builder included with this package, it will create a database view or an SQL view using the name that you provide, and it will merge in
04:39
the two relationship things that we want to pass to it. In our case, this is going to be the user's accepted friends to and the user's accepted friends from, although this could be absolutely anything. So if you needed to use this package for something else, it would work too.
04:56
So let's go over and create out a migration to handle this, so let's say make migration create friends view, and let's go over to create friends view, and we're going to get rid of this default schema for both the up and the down. We're going to get rid of the namespace here because we're going to use that custom schema
05:19
builder, and we'll just copy and paste this in. So let's paste this into up. Let's put in the correct schema builder here. We might just need to re-index, and let's try that again.
05:30
So this is going to be Laravel merged relations, facades, and schema. We need to give this a name, so I'm going to call this friends underscore view because we've already got a table called friends, and now let's just pass them two relationships into the second argument here, which is just an array.
05:46
So we're going to say new user accepted friends to and new user accepted friends from. Just make sure we pull the namespace in for here, and that's pretty much it. So that will merge them two things in and create this view out for us, which we'll take a look at in the database.
06:06
Now for the down migration, if you do want to fill this in, you can go ahead and drop that. So let's go over and say schema drop view friends view. So we're ready to go ahead and migrate and see what this creates for us.
06:22
So let's run migrate, and let's go over and look at this view. So you can see here that we have got the two friends in here that have been accepted from either side along with some other columns in here, which help piece this together. If we look at the structure of this, this is the view definition that's been created
06:41
by that package. We don't need to necessarily understand this unless you want to, but it's there if you want to take a look. Okay, great.
06:48
So we've got the view, but how do we actually use this within our user model? Let's go over to our user model, and we're going to go ahead and create out a friends relationship in here, much like we would normally do, but this time we need to fill this in specifically with the package that we have created.
07:06
So before we do this, if we just head over to here, we need to use, if we just head up here, the, let's find it, it's probably down here somewhere, the has merged relationships trait. So let's go up to the top of our class and use this in here, and then let's go down and
07:27
define this relationship out. So to do this, we go ahead and use merged relation with model, and then we just pass in the model itself because, of course, friends are users, and then we just pass in the name of the view that we've already created, which is friends underscore view.
07:45
That's pretty much all we need to do. So now what we can do is head over to our friend index controller, and we can pretty much just bring this back to how we had it before, but we don't need to call a method anymore because this is now a relationship, which if we call like this will return to
07:59
us a Laravel collection. So let's go ahead and just see what this looks like. Hopefully we're back to how we saw it before, and over on Alex's account, Alex has Mabel as a friend, and if we head over to Mabel's account, she now has Alex as a friend.
08:14
Now this is so much better because it means that we could paginate this information, we can access relationships via this friend's relationship if we wanted to, so this makes a lot more sense. And of course, if we add other friends, they're going to appear here as normal as well.
08:29
Just before we go, let's test that out. So I'm going to go ahead and create another user in here called Tabby, and let's go ahead and see if we can add Tabby as a friend. So that hasn't worked.
08:41
Let's make sure we... Ah, okay. We're in the view here. Let's go over here and add this in here, so Tabby, and let's fill this in.
08:50
There we go. So for Alex's account, let's go over to profile slash three, add Tabby as a friend, and let's just manually accept this in the database. So if we just come over to friends here and accept that, what we should now see over in
09:11
Alex's friend section is he has Tabby, and of course, if we logged in as Tabby, Tabby should have Alex. Tabby could add Mabel as well if you wanted to. This package will basically just merge these in, and we will see everything as normal.
09:25
Okay. That was fairly heavy. Hopefully, that makes sense. What we're now going to do is look at unfriending a user in the next episode.
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!