33. Merging friends properly


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,
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.
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.
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.
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,
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.
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,
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.
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,
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,
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.
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.
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,
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,
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.
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.
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.
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.
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
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.
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.
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.
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.
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
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.
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.
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,
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
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,
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
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.
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,
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
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.
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.
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
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
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
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.
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,
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
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
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
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,
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.
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.
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.
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,
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.
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
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.
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,
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
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


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!


No comments, yet. Be the first to leave a comment.