30. Accepting a friend request


In this episode, we are going to look at accepting pending friend requests. So we know that Alex has already sent a request over to Mabel. So over on Mabel's page, we get a list of friend requests, and over here, we get our pending friend requests.
So over on Mabel now, we want to be able to click a button to accept this friend request. Of course, we're going to start out with a test. So let's go ahead and build this out now. So let's go ahead and create this out as friend patch test
because we're going to be updating the existing pivot. So we're just going to create a patch controller. And of course, we're going to go ahead and pass our pest flag in there. So let's open up friend patch test.
And the first thing that we're going to do is come over to one of our other feature tests and just grab this redirects unauthenticated users as well as our refresh database stuff. So we can just put this straight into here, and we're pretty much ready to go. Okay, so we want to go through to friends, but we want to make a patch request.
Let's run pest on this test specifically. So feature and friend patch request or patch test. And of course, we get a 405. This doesn't exist or in our case, it can't find the patch.
So it is returning a 405. So let's go ahead and create out a friend patch controller. Let's do that first of all before we define the route. Make controller friend patch controller.
Here we go. And let's switch this out in here. Friend patch controller. Great. So let's open this up, make our test pass, and we should be good to start implementing the functionality.
So to make our test pass here, we're just going to implement our auth middleware and that should be enough. So it doesn't look it's working at the moment. Let's see. We've still got a 405 in here. Did I save out that web route? Let's try that again. And okay, invalid route action.
Of course, that's the reason. Let's go to our friend patch controller and just put an empty invoke in there. And let's try that again. Okay, so that's working. Now what we actually want this to happen,
if we think about it, is not to just patch over to friends. We want to actually pass the friend in here to grab that value. So let's go ahead and add that friend in there. And then over in our friend patch test, let's go ahead and just pass any ID in here
because for the purpose of that test, it doesn't really matter too much. Okay, so the next thing we want to do and pretty much the only thing we want to do in here is check that it accepts a friend request. It's only going to update an existing pivot.
So let's go ahead and try this out. So it accepts a friend request and let's go ahead and create our closure in here and let's get started. So we know that we need a user to actually make the request. So let's go ahead and create that user out.
We know that we need a friend in here to be able to be added. Pull that user model in and we want to just go ahead and for that user add a friend prior to this so we can accept it down here. So acting as that friend,
remember because the friend is going to accept the user's request. We want to go ahead and make a patch request over to friends slash and then we're just going to use the user's ID in this case, although you could use something a little bit less revealing about your database.
The next thing we want to do is just assert that the database has inside of that friends table the user ID who made the request which is the user ID the friend ID who is added. So in this case that is just a friend ID
and really importantly the whole point of this test is to make sure that accepted is true. So let's run this test to see what happens. And of course it can't find that in the database. Accepted is still zero.
So once we've implemented the functionality of it in here, this should work nicely. First things first is we want to accept the user in here. So let's pull our request in first and this user we called friend within the root model binding. So we're going to have to name it friend just in here.
Now what we can do is for the currently authenticated user and the pending friends of we can go ahead and update the existing pivot for this for that friend that we're passing in and set the pivot relationship in here or the pivot column to accepted true.
And then of course down here probably just return and redirect back. Okay, let's run our test and we get green. So we know that we can hit this endpoint to accept a friend request. Now all we need to do is hook this up in the UI.
So let's head over to our friends index and let's go down to where we have our friend requests. So pending from requests is for the user who's adding we want the actual friend requests and let's pull this in and tidy this up a little bit.
I'm actually going to go ahead and add the email address of the user in here as well. So let's say requesting friend and email get rid of that extra dollar and there we go. So we want a little button here to accept this. Now because we have gone ahead and set this as a patch request,
which makes more sense in terms of rest. We're going to need a form here with a button rather than just a link to go through. If we did have a link to go through that would also mean that we couldn't use cross-site request forgery protection
to stop someone accepting a friend request on someone else's behalf. So the action here is going to be friends. We already know this because we've written our test and it's going to be the requesting friend ID.
The method for this form is still going to be post, but because we're using a patch request, we're going to have to spoof this with Laravel and use patch inside of here using this method directive.
Now we can just have a button in here that says accept and we can style this up to look more like a link by just setting the text color to blue 500. Let's go over give that a refresh and there we go. So you could make this all on one line if you wanted to
by setting this form with a class of inline. It's entirely up to you and that goes up to there. But the most important thing now is that when we click this, we know that it goes ahead and accepts the friend request.
We now have Alex in our list of friends and over here we have Mabel in our list of friends. These two are now friends. Great. So let's just update our friends list really quickly with the email address as well
just to keep some consistency here. So let's look for friends up here and we can go ahead and just pop that in there. Later on we'll be adding a button to remove a friend
if we actually put friend in here rather than requesting friend. There we go. So we have got two people that now have been friends through that entire process of adding and accepting.
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.