In this episode, we're kicking off the main part of the course: building out the friend functionality. We start by writing a test before doing any database or model work. This helps us design how we actually want the friendships to work in our app, thinking through what methods and relationships we'll need on the user model.
We set up a unit test for pending friends, deciding how an "add friend" action might work in code, and assert that the pending friend requests are tracked correctly. This means creating two users and having one add the other as a friend, then checking that they show up in a pending state.
Next, we set up the database with a friends
table to track friend requests — including who sent the request, who received it, and whether or not it's been accepted yet. We use Laravel's pivot tables and relationships, like pendingFriendsOfMine
and pendingFriendsOf
, making sure it's just as easy to see who we've sent requests to and who has sent requests to us.
We talk through a bit of refactoring to keep the code readable and flexible, and test both sides of the friend request — sending and receiving — so we know our relationships work as expected.
By the end, we have all the groundwork in place for a robust friend system: users can send and receive friend requests, and we know exactly who's waiting for who to accept!