Playing
33. Merging friends properly

Transcript

00:00
Before we go any further and start to look at building and testing our feed, which of course is going to show all of the books that our friends are reading, have read, or want to read, we really need to fix up this friends relationship. There's also another bug here which hasn't been caught in our tests,
00:16
which we're going to cover once we have sorted this relationship out. So just a reminder, what we're doing at the moment is taking the collection for the accepted friends of mine and the accepted friends of, and we're just merging these two together.
00:29
Now doing that gives us back a collection, which is fine. We can still iterate over that, and this works nicely. But this isn't a relationship, so we can't use this as a relationship. We can't easily paginate it.
00:41
It's really a little bit restrictive. So what we're going to do is we're going to look at how we can merge these two with a view within our database. And to do this, we're going to use the Laravel merged relations package.
00:54
So what this allows us to do, if we just scroll down here to our view, is create out a database view very, very easily within a migration, and merge two things together. Now for this, this would be a user's accepted friends and accepted friends of,
01:11
and then we can reference this view name within a relationship. And this will return to us a relationship which we can continue to query on, and we can paginate and all of that kind of stuff. So don't worry if this doesn't make too much sense.
01:22
Of course, what we're going to do is look if this still passes our tests after we've gone ahead and switched this over. At the moment, all of our tests are passing. So let's go ahead and install this package, switch it over,
01:33
and then see what happens. So let's go ahead and grab the command to install this, come over to our terminal, and just pop that in there. And we first of all need to create the migration.
01:43
So if we just come down to the view here, this is pretty much what we need to do within a migration. So let's do this really roughly for now. So let's go ahead and say phpArtisan make migrate,
01:53
and we're going to create out the friends view table or friends view view. Okay, so let's go over to create friends view view, and let's go ahead and fill this in here. Now, Laravel's populated this with all of this stuff,
02:09
but we don't actually need this. So inside of here, let's just make sure we index our workspace. We want to go ahead and use schema from this package, and that's renamed that just because we already have the schema in here.
02:22
So I'm going to go ahead and get rid of that and remove that alias. Let's come down and just switch that to schema. And then we want to go ahead and create a merge view, give this a name. So in our case, we're just calling this friends view.
02:35
And then we want to pass the two things that we want to merge in, which is both ends of the friends, because remember, they can come from both directions. So inside of this array, we're going to go ahead and new up our user model,
02:48
and we're going to say accepted friends of. This is only accepted friends, really, really important. Let's pull our user model in here as well. And the next one, if we just copy and paste this,
02:59
is going to be accepted friends of mine. So that's going to merge them two together within a view. And we'll check this out in the database once we have run our migration. So let's go ahead and say migrate, and we have a little error here.
03:12
Let's just have a look. Okay, so it looks like this has failed. And yeah, I think we've pulled the wrong namespace in here. Yeah, we've done this from the Laravel migration views package.
03:21
So let's just come back down to here and choose the right one. That is going to be the merged relations package. Okay, let's go ahead and just try that again. And there we go, great.
03:31
So what that will have done is created out a view in our database table. The moment we don't have any friends in here, this view, if you're not familiar with SQL views, is kind of like a studio table that contains a query.
03:42
This has been built up with this package. And this will give us the friends relations in here as these are added to this friends table. So let's go ahead and just add a friend in here
03:51
and just see what this looks like. So from Alex's account, I'm going to go ahead and add in Mabel. And there we go. And let's go over here and accept this.
04:00
So they are both now friends. And if we just come over to our friends table, that's in there. And under the friends view, we now have these two records that are attached to both of them users.
04:12
So now Mabel has Alex as a friend and Alex has Mabel as a friend. So what we can now do is go over to our user model and actually change around this relationship. Remember, at the moment, our tests are still passing.
04:24
Let's see if they pass after we've switched this over. So instead of returning this merge, we're going to go ahead and say this. And we need to pull in the facade, the trait for this.
04:35
So let's go ahead and do that now. Use merged relationships or has merged relationships. Let's go down and say this and merged relation with model. We're going to go ahead and choose user
04:47
because remember a friend is a user. And then we're just going to pass in the name of the view that we have created, which is friends view. So let's run our tests and see if this passes.
04:57
And it doesn't. So let's just have a look. Let's go up here. And it looks like it's still not working.
05:02
And let's have a look here. We're actually still using this get friends attribute. Remember, this returns a relationship. So now what we can actually do,
05:11
rather than have this as a relationship, is switch this over to friends. And then when we access friends on a user, that's going to automatically return a collection to us
05:20
like any other relationship does. So now that we've switched that over, let's run pest. And sure enough, all of our tests pass. If we come over here,
05:27
you can see that both of these friends are now in there. And this is working in exactly the same way. Now we actually had a bug before. If we just go over to our user model
05:37
and go up to remove friend, what this is going to do, we didn't catch this within our test, is remove a friend from friends of mine.
05:47
Now what that's not going to do is remove it from friends of. So we can actually recreate this over in the browser. If I come over to Alex's account here and hit remove,
05:57
that's going to work because that relationship has gone this way under friends of mine. But if the other user who has been added, which is under the friends of relationship
06:07
tries to remove this, it's not going to work because this relationship won't return anything. So let's just try this out by just re-adding Mabel as a friend.
06:16
We know that we can delete Mabel from this side. So if we come over here and accept this, that's now working. But over on Mabel's end, if I hit remove, nothing happens.
06:26
We're just redirected back because if we remember over in our friend destroy controller, we're just saying remove friend, which of course is just accessing
06:35
that friends of mine relationship. Now, unfortunately what we can't do is access this friends relationship because this is technically giving us the data
06:44
from the view, which we can't control because this is constructed from our actual table. So if I now try and remove this from here, you can see that we don't have a method on here
06:53
to actually remove that. So what we're going to have to do in this case to fix this issue up is just go ahead and cater to both of these.
07:01
So friends of mine and friends of. So we're just doing this twice basically just to cover both of these cases. Now, if we just have a look,
07:10
we've got accept friend, which is fine and add friend, they both work nicely, but this one just needs fixing up. And we could go ahead and update our test
07:18
to account for this as well. So we'll maybe do that in just a second. Okay, so let's just test this on both sides here, hit remove, that removes that friend from the other side
07:27
as we saw before. Now let's just read Mabel in here and hit send request, accept this from here, but then Mabel decides that she doesn't want Alex
07:37
as a friend, so she hits remove and there we go, that's gone. So we can cover this in a test very, very quickly. If we just head over to our friend test here under unit,
07:47
we could really remove this, but let's go and have a look at can delete friend. Did we have this in here? No, we didn't.
07:56
So let's go over to our friend destroy test and yeah, deletes a friend request. So let's just duplicate this test down and just test this from the other way around.
08:05
So deletes a friend request from the added user, I'm sure you can think of a better name. So let's just change this functionality back to how it was just by commenting this out.
08:17
We'll build the test, make it fail and then we'll put that other call in there just to make this pass. So we have user added a friend,
08:25
this time we're gonna do exactly the same thing, but we're gonna act as the friend and delete that user instead and then check that this is missing.
08:32
So we're just doing it in the opposite way around. Let's run our test and we'll specifically run this one. So under feature and friend destroy test and of course that fails
08:43
because this is still in the database. So if we just come over to our user model again, pull this back again and rerun our test, you can see that passes.
08:50
Let's just finish up by running all of our tests just to make sure and there we go. So the whole point of this was just to really fix up our friends relationship, which we know is now working,
09:01
we can see all of our friends on this page and that's gonna really help us when we start to build up a list of our feed because of course we want to see
09:10
all of our friends in there, not just the people we've added or who have added us, we wanna see all of them books listed on our feed, which we're gonna work on in the next couple of episodes.
35 episodes4 hrs 19 mins

Overview

Pest is a PHP testing framework that brings beautifully simple syntax to your tests, without sacrificing on features. In this course, we'll get up and running with Pest in a Laravel project and write tests for a real-world application that we'll build along the way.

You'll learn how to set Pest up in a Laravel project, write tests with Pest's built-in assertions, generate code coverage, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!