This episode is for members only

Sign up to access "Build a Friend System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
04. Building the user profile page

Transcript

00:00
So next up we're going to build the user profile page because this is where our link is going to
00:04
be to actually add a friend. So we're going to need to be able to click on that button, submit a form and add a friend. There are going to be lots of different ways that you can add friends. For example, you could enter a user's email address to add them as a friend. We'll kind of get to how we would do that later in the validation rules around that. But for now,
00:22
let's just build out this really simple profile page. So first of all, we'll start with a controller. So let's just say profile index controller. And of course, we'll head over to our root and we'll create this out. So let's just copy this one down. And we'll go ahead and say profile slash, and then we'll use root model binding to pull that user in. Let's go ahead
00:43
and say profile index controller. And we'll just call this profile. Bear in mind at the moment with root model binding, this by default will accept in the ID. If you did have usernames, you'd want to go ahead and maybe choose the username column for that rather than expose the IDs of your project. So over in the profile index controller, we don't need any middleware for this, but we do
01:04
need an invoke magic method. And we're just going to return a view out here and just call this profile index. And once again, just come over and create this. So let's create a profile folder, index.play.php. And we'll just put profile in there for now. Okay, so inside of invoke, we're going to grab a user that's looked up by root model binding. And of course, we're going
01:28
to pass that down to our view. So we can actually output the details about that user. And just make sure we pull the namespace in for this at the top. Okay, so we've only got one user at the moment here. So let's just duplicate this down and just give this another name. And of course, another email address because they're unique. And let's go over, that should be ID of two,
01:51
and it is. And let's go to profile slash two. And there we go, we get the profile details. Now, once again, we're just going to come over to the dashboard page, copy this over to the profile index template. And at the top here, we'll just output the user's name just to keep things really, really simple. So user and name. And we should now see Mabel at the top. Now, of course,
02:14
we don't want to see you're logged in. This is where all of our button actions are going to go. Let's scaffold that out now really quickly, just so we've got something to come back to. So we're going to create our form, the action we don't know at the moment, the method is pretty much always going to be post, we're going to go ahead and use our cross site
02:31
request forgery directive to pull in the cross site request forgery token in there. And we're going to have a button. So the first one that we're going to do is as add as friend. And we'll add some color to this because at the moment, it just looks black. And let's say text indigo 600. There we go. So when we submit this, it's going to go ahead and make a post request,
02:56
which of course, at the moment, does not work. So we'll be changing this up depending on the status of the from request. And of course, we will come back later to look at profile one, which is us, because we shouldn't be able to add ourselves as a friend. So we'll be protecting against that as well. So there we go, a very, very simple profile page with our add as friend button.
14 episodes1 hr 16 mins

Overview

Everything you need to implement a friend system in Laravel, built in a simple UI so you can integrate it into your existing apps.

We'll start with the basic friend relationships, then add in more advanced relations to make things future-proof. Want to easily get all friend's statuses on a timeline? No problem.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!