In this episode, we start building out the ability for a user to add a friend—in this case, Mabel. We're keeping things simple for now, without any fancy validation or checks (we'll get to those in the next part).
We create a FriendStoreController
in Laravel to handle the "add friend" request. Right away, we make sure only authenticated users can perform this action by applying the standard auth
middleware. We set up route model binding so the controller can easily get the user being added as a friend, along with the currently authenticated user.
After sorting out the route and updating our profile page with a form (not just a link, to ensure we have CSRF protection!), we do a quick test to dump out the user data and confirm everything is hooked up correctly.
Next, we actually make it possible to send a friend request by attaching the selected user as a 'pending friend' to the current user. We also make sure to record timestamps in the pivot table, so we know when the friend request was made.
Finally, we discuss a couple of improvements for the UI to indicate when you're waiting for someone to accept your request. But there's also an important caveat: right now, you can keep sending the same friend request over and over! We'll tackle that issue with some extra checks and user feedback in the next episode, so stay tuned.