34. Getting books of friends


Before we go ahead and actually build this feed out, we're going to create and test the
relationship that gets the books for our friends, so we basically want to see all of the books that belong to our friends including the statuses, we want to make sure this is ordered properly as well because we want to see the latest book that's been updated at the top of the feed, so for example if one of my friends has five books and they update one to say that they're
currently reading it, I want to see that at the top because it's the latest kind of event, now all of this sounds pretty complicated but we're going to use this new merged friends relationship coupled with another package which is this eloquent has many deep to achieve this, so the reason that we need to do this is let's think about our database structure,
we've got a user who can have many friends that's via this friends view but they also have many books but that's via this book user relationship and we can't actually do this directly within Laravel at least at the time of recording, so we're going to need to lean on this package that I've just mentioned to do that, now before we even touch this package we're going to write
out our test so we can actually figure out what we're trying to look for, so we're going to do this under our friends unit test and we're going to create out another test in here and just say it can get books of friends, I think that just makes sense and we'll try and include as much as we can in here, so we'll include things like adding users, so user 1 and user 2, user 2 adds user 1
and user 1 adds user 2 so we can get both directions of that relationship in here just to make it pretty thorough, so let's just build up some of the users that we're going to test within here, we're going to have a base user here and then we're going to have say two friends so let's create friend 1 and let's create friend 2 in here, so that should be just about enough,
what we can do is maybe create another one and not add a friend in here as well, so let's say friend 3, so what we'll probably end up doing is user here adds friend 1, friend 2 adds user so that's the sort of multi-direction relationship and then we add friend 3 but they don't accept and we want to make sure we don't see any books of friend 3 because they've not accepted that
relationship yet, so although that's a little bit verbose I think that's a really good way to approach this test, so let's go ahead and do this now, let's say friend 1 books and let's add in a book, so we'll call this book 1 so we can keep an eye on this, so book factory and create and that's really getting in the way, not forgetting that we need to attach the pivot data for the
status which doesn't really matter in this case, we will test that we can get this back but it doesn't matter what status it is, let's do the same for friend 2 as well and again this is getting a little bit verbose we could refactor this but we'll perhaps look at that later, so we've got book 2 again we'll say that that's want to read as well and let's add another one for friend 3
because we don't want to be able to see that in there, so let's say book 3, great okay so we've got three friends with a book, now we need to think about adding these now, so let's say user add friend, friend 1 and let's say that that friend accepts our friend request, so accept friend user, now let's do the reverse which is the issue we looked at in the last episode,
so we're going to say friend 2 adds that user and that user accepts friend 2, so we should now have two friends either way, so this user should have this friend and then because the this friend has added us we should have another friend there and then lastly what we'll do is just add a friend 3 but we won't accept that, so friend 3 won't accept that, so we should only have two friends and we
shouldn't see friend 3's book in this feed, so let's look at a pretty basic expectation here and let's say user and we need to think of the relationship here, so let's just call this books of friends, I think that kind of makes sense and we want to make sure the count of this is two, so let's do that, okay so let's just go ahead and run our test here, so pest tests and we've put
this under unit friend test and of course we get a failure here because we have a count on null, that just means that this relationship doesn't exist yet, so we're going to add some more expectations in here in a moment but let's look at actually adding in this eloquent has many deep relationship so we can actually get books of our friends, so let's go ahead and pull this package
in and let's go over and see how we use this, so we want to use an existing relationship because we've already got, if we just open up our user model, we've already got this friends relationship in here so we may as well lean on it and use the functionality from this package, so for this what we're going to need to do is use the has many deep from relations package or method, pass in the
friends and then pass in the books so we know what we're getting from them friends, okay so we need to go ahead and use this has relationships trait, so let's come over to our user model just at the top here and say use has relationships and let's just go ahead and pull this in from this package, great and let's come down here and create this method out, so call this books of friends and
then let's go ahead and pull this in, so this has many deep from relations, it's already there for us, this friends and then as part of this we just need to go ahead and new up a user model just to grab the books relationship, this is that isn't actually grabbing any books it's just grabbing the relationship type so this knows what to do with them, okay let's go ahead and run our test again
just to see what we get here and yeah sure enough it passes we have two books from our friends, now that's not quite enough because we need to know a couple of other things about this and we need to be really really careful, so we need to make sure that the first book is the latest that's been updated, so let's pick one of these books that one of our friends has created, remember
friend three hasn't been added yet but we want these two to let's find out where we are and these two need to be in a certain order, so maybe for book two we can update this with an updated at date like so to be maybe sub a day, so let's remove a day from that, so this one was updated a day ago and this one was updated just now, we could make that a little bit clearer in our test by just
saying updated at now if we wanted to although we already know that updated at is filled to the current date and time, so this book one should be at the start so it should be the first book, now the reason we've done it in this way is when we add a book here this one and we add this one second this one is most likely to be by default at the top so we're basically pushing this one back
and we're going to test that book two is the first thing that we get back from here, so let's go ahead and say first using pests higher order assertions here and let's say title to be and book two title so we want book two to be at the top of the collection that we get back, let's run our test and you can see sure enough it fails, now the reason it fails is because we haven't set up any kind of
ordering on this custom relationship so we're going to go ahead and do that now, now actually just before we do this let's just die dump on the collection that we get back here so let's grab this and just die dump this within our test and run our test just to see what we get, so if we just go through here we have got updated at but that's the book itself that's not what we're
after we're updated we're after the pivot updated at but it doesn't look like we have this information in here so what we're going to do is over in our user model we're going to go ahead and say with intermediate and we're going to pass in that book user pivot class that we created earlier now if we go ahead and run this again you can see that we'll get a lot more information here so
let's go up to the top here and this is now contains all of the pivot information which is prefixed by a double underscore the pivot name and then the actual pivot column so we of course want to go ahead and order this by book user updated at so we can go ahead and do that now so let's say order by book user updated at and we want this in of course descending order let's
go ahead and rerun our test here and let's get rid of the die dump of course first of all we'll actually delete that and let's run that and okay it still doesn't work so let's figure out why that is let's go over to our user model here and maybe we didn't quite write our test correctly so book two is sub a day but oh okay so book one should be first shouldn't it of course okay let's just
revert what we've done so we know that this doesn't affect anything and let's say book one title okay so if we run our test we know that this should fail it actually passes so we want to make this fail so maybe let's add a day to this one to make this one the top one and then let's go down and specifically say book two we basically want this to fail first of all and it does okay great so
that's just due to the way that the records are created and laravel's default ordering we want to kind of prove that it's not working for the default ordering before we go in and specifically order this in the way that we want hopefully that makes sense okay let's just pull back what we added before and when we run this it passes great so we know that by default
without the specific ordering that we've given this fails just with a little bit of tweaking of our tests and then of course when we add in the ordering just here we know that that now works okay so we now pretty much know that we can get from our friends a list of our books let's now go ahead and look at building out and testing the actual feed over in the browser
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.