In this episode, before we jump into building out the feed UI, we focus on the backend logic needed to get all the books that belong to a user's friends. The idea is that when you view your feed, you should see the latest activity from your friends—including which books they’ve updated or started reading—sorted so that the most recently updated ones are at the top.
We start by planning an in-depth test that covers various friendship scenarios: users who are confirmed friends in both directions, and users who haven’t accepted friend requests yet (whose books shouldn’t show up). We create users and books, set up their friendships, and write expectations for what should appear in the "books of friends" list. This involves making sure only the correct books appear, and they're ordered properly.
To achieve this complicated relationship in Laravel, we use the has-many-deep
package, since Laravel doesn’t natively support this kind of deep relationship querying at the time of recording. After setting up the necessary relationships and tweaking the test, we figure out how to include pivot data (like the book-user pivot's updated_at
field), and use that for proper ordering. There's some fiddling around with test expectations and data, to confirm that our custom ordering is actually needed and works as intended.
By the end of the episode, we’ve got everything in place to fetch all our friends’ books, with status and the most recently updated ones showing first. Next up, we’ll be ready to build the actual feed in the browser!