در حال بارگذاری ...

Add User Relationship0:06

All right, welcome back. You know what? I think so many doors are going to unlock for you beginning in this episode. So let's jump right in. So within my editor, let's return to what an idea consists of. And initially we kept it very basic. It just has an ID and a description and that's it. But now since we have authentication in place, I want every idea to be associated with a user. That way I can see John's, uh, ideas versus Jane's,

I want every idea to be associated with a user. That way I can see John's, uh, ideas versus Jane's, or we can at least, uh, make them private. So let's do this. I'm gonna say I need a foreign ID for the user id, but we can also use this foreign ID four method that allows us to ref, uh, to reference an eloquent model. So now I can say, okay, give me a foreign ID specifically for this relationship right here. We can also stack onto this.

for this relationship right here. We can also stack onto this. I can say I want this relationship to be constrained and we should cascade on delete. Alright, so here's what this means. Imagine a scenario where a user signs up and they create 50 ideas and then we delete that user's accounts. Alright, well now we have 50 ideas that are not associated with any user right there orphans.

Alright, well now we have 50 ideas that are not associated with any user right there orphans. So instead we can say, all right, let's create a a database constraint and on the condition that the user is deleted, let's cascade and also delete all of the ideas for that user and the process. Okay, so now I've made a change to my original migration and that's fine at this stage of our project. So I'm gonna refresh our migrations from scratch.

Refresh Database Migrations1:31

and that's fine at this stage of our project. So I'm gonna refresh our migrations from scratch. PHB Artisan Migrate Fresh, and yet, just keep in mind that will also wipe your, uh, your records as well. But that's fine. And you can also, by the way, research database cedars to quickly scaffold your database with dummy data, uh, for local testing and such. Okay, so if I come back, let's do a little bit of housekeeping.

Protect Routes with Middleware1:53

Okay, so if I come back, let's do a little bit of housekeeping. Now, I'm not currently signed in, however, I see a link to create a new one. So if I click on this, you can see I've updated our new and edit forms to match our login form as you see right here. And that's cool, but also I shouldn't see this form if I'm not signed in, right? You have to be signed in in order to create a new idea.

I shouldn't see this form if I'm not signed in, right? You have to be signed in in order to create a new idea. So let's switch back to our routes file. So if you think about it, every single one of these routes should require an authenticated user. It doesn't make sense to create an idea if you're a guest. It doesn't make sense to edit an idea if you're a guest. So here's what we can do. We can reach for this middleware method and call auth. Auth refers to a native middleware

We can reach for this middleware method and call auth. Auth refers to a native middleware provided by Laravel that just says exactly what you think you have to be authenticated in order to access this endpoint. And we're using middleware for that. Middleware is almost like layers of an onion leading to the core of your app. And you can have one or more middleware that basically protect, uh,

And you can have one or more middleware that basically protect, uh, or at the very least, the request has to travel through in order to get to the core of your application. So in this case we're saying you gotta travel through this off middleware before you get to my application. So if we visit that endpoint, it fails as we would expect. We in this case, well route login, not define what's going on there.

We in this case, well route login, not define what's going on there. Well, we asserted that this endpoint, the ideas page requires authentication. So if you are not authenticated or signed in, it's going to redirect you to this page. But the way Laravel handles that is it tries to redirect you to a name to route called login. And we don't have that right now. So lemme show you something.

And we don't have that right now. So lemme show you something. Let's return to the sidebar and let's open up this bootstrap folder. This is responsible for bootstrapping your application and we'll go into app. Alright? So we can say for middleware, middleware, and we are going to redirect guest to the login page. So by default it redirects them to a named route for login, and I'll show you how to add that as well.

So by default it redirects them to a named route for login, and I'll show you how to add that as well. But alternatively, you can override this to point anywhere you want. So now if we were to give it another shot, you'll see that, uh, it redirects. And actually let me show you this. Here we go. If you're on a different page and you try to access that, it redirects you to the login page.

and you try to access that, it redirects you to the login page. Alternatively, we can just simply add a named route. Think of a named route as a name for a URI that allows you to reference that route without referencing the URI specifically. And this allows you to then, uh, for example, change the URI without breaking every single uh, URL that sends you to that route, right? You can, you can separate them.

that sends you to that route, right? You can, you can separate them. Basically, I can say right here, this has a name of idea show and now I have a way to point to this route and to this endpoint and this request type, um, without actually hard coding. The URI that said, I'm gonna be honest with you, I'm not the biggest fan of named routes and I typically don't use them anyways though. If you do wanna make Laravel happy, in this particular case,

and I typically don't use them anyways though. If you do wanna make Laravel happy, in this particular case, you would just assign a named route of login to whatever route represents your login route. So I could say, all right, this is the one that represents login. And you can see why Laravel does this. By the way, what if your login route was something like session slash creates? Well, Laravel doesn't know that that's what you named it.

session slash creates? Well, Laravel doesn't know that that's what you named it. So instead it just wants to redirect to whatever route you have assigned this name to. And that's why it does that. Anyways, good to know. So if I come back and, and, uh, let's say we're on the create page. If I click on this, it redirects us to the login. Very cool. So now you've learned how to selectively apply the off middleware.

So now you've learned how to selectively apply the off middleware. We can also do it as a group. I can say route middleware off and we're going to apply it to this entire group of routes, and I will nest those within here. Now we can also do the inverse. What if I instead want to assert that you are a guest, right? Um, does it make sense if you're signed in

that you are a guest, right? Um, does it make sense if you're signed in to access a registration form? No, it doesn't. So we shouldn't allow it. So once again, I can do it selectively like this middleware. And let's make sure in order to access the register endpoint, you are a guest. And if you're not a guest, we're gonna redirect you somewhere else. That's the way that works. So we do the exact same thing

we're gonna redirect you somewhere else. That's the way that works. So we do the exact same thing here, and then you can also imagine us doing it for these two routes as well. So with that in mind, once again, why don't we use a grouping? So I will copy this and this time this will be our grouping for our guest specific routes, which would include registration and login. All right? And then log out, once again, would require off.

specific routes, which would include registration and login. All right? And then log out, once again, would require off. So yeah, if you like, this can be one way that you organize it. Alternatively, if you just wanna apply off middleware to every single one of these routes, that's fine too. And some people prefer that. Cool. So let's come back. Can I see the login page as a guest? Yes. Can I see the registration page as a guest? Yes. All right, let's register. John Register.

Can I see the registration page as a guest? Yes. All right, let's register. John Register. All right, he signed in, which means he cannot access the register page. So in this case we get a 4 0 4 because look, it tried to redirect us to the homepage and right now, currently we don't have a homepage. So yeah, what we could do real quick until we actually work on that, we could say, alright, return placeholder for homepage

until we actually work on that, we could say, alright, return placeholder for homepage and that would be your marketing page that you would work on, right? Come back and refresh. The important thing is that works. Now, alternatively, if you just wanna say, alright, redirect, if you're signed in and you try to access a guest specific route, redirect them to slash ideas, then you can do that as well. Once again, from Bootstrap app, I can say redirect guest

to slash ideas, then you can do that as well. Once again, from Bootstrap app, I can say redirect guest to login if you want. But also redirect users who are, um, accessing a guest route to ideas in this case. So the point is, you can configure this however you need to. So refresh, let's try to access register and you'll see, nope, it just sends you right back here because of that middleware cool, you're learning new stuff. All right, so let's create a new one.

Assign Idea to User8:21

because of that middleware cool, you're learning new stuff. All right, so let's create a new one. And now at this point, if I try to submit this, it's gonna fail, right? So I give it a go and we get a database, um, error, a query exception because now we're trying to store a new idea. But an idea requires a user and we didn't create that association. And you can see it right here. All right, let's fix that.

and we didn't create that association. And you can see it right here. All right, let's fix that. Let's go into idea controller. And when we store a new idea, I'm gonna show you this in two steps. First, of course, you can just reference the user id, uh, that is associated with this idea. And if you think about it, the user ID should always be the ID of the currently authenticated user. So you can use that off facade

ID of the currently authenticated user. So you can use that off facade and say, give me the current user's id. That's pretty cool right? Now you might be wondering, well what if the user's not signed in off ID could be null, right? No, it can't remember right here, we already declared that, Hey, in order to access that controller action, you have to be signed in.

Hey, in order to access that controller action, you have to be signed in. So we can always assume that we have access to the current, uh, authenticated user. We can grab their ID like this and we can grab the user instance like this. Don't forget this, write it down. So if we were to give this another go, it works. Now I have an idea associated with, uh, my current user id.

Now I have an idea associated with, uh, my current user id. Let's have a look within my ideas table. Sure enough, I can see that this record is associated with the user with an ID of one. Okay? But now, and let's redeem this real quick. Build a house or something like that. Let's imagine though that that was created by a user with an ID of two. At the moment though, I don't have any other user.

with an ID of two. At the moment though, I don't have any other user. So here's what I can do. If I run PHP Artisan tinker very quickly I can reach for my user model and say, call this factory method to quickly whip up, uh, effectively a dummy user record and then persist it in the database. So you can see it's grabbing fake data here. And this is incredibly useful for local development and especially testing down the road.

And this is incredibly useful for local development and especially testing down the road. So now we've learned just a tiny bit about model factories back to table plus refresh. And now we have two users. Okay? So let's imagine Cecilia actually created this idea. Uh, and I will this idea and I will hard code it. Like so we just have one problem back in the browser. If I refresh, well, I'm seeing an idea that I didn't create. Alright? So now we need some filtering, right?

Filter Ideas by User10:53

If I refresh, well, I'm seeing an idea that I didn't create. Alright? So now we need some filtering, right? I should only see ideas that were created by me. Alright, let's do it. Now let's go into the index action. And sure enough, we're just fetching everything from the database that doesn't make sense. So once again, put a pin in this. I'm gonna talk to you about relationships in the next episode, but for now, we're just gonna do it in long form. I'm gonna say let's build up a new query

episode, but for now, we're just gonna do it in long form. I'm gonna say let's build up a new query and give me only the ideas where the user is myself and give me a collection of the results, okay? And this is what we want. I don't want all ideas in the database. What if I have a thousand users signed up? Do I get all of their ideas? No. In this case, ideas are private. So I can only see my own own. Okay?

In this case, ideas are private. So I can only see my own own. Okay? So now if I switch back, give it a refresh, I don't see anything 'cause I didn't create that idea anymore. Let's create one for me. Um, work on a project, what a great unique idea. And now I can see, alright, this one belongs to me, but that other one that belongs to Cecilia. Great. Now why don't we do this?

but that other one that belongs to Cecilia. Great. Now why don't we do this? If we have ideas, I don't see that create link, which is kind of annoying. So, uh, at this resolution, I'd have to open up the nav bar or maybe we can just display one here. So why don't we say right here, let's just move this onto its own paragraph. There we go. And then MT six, add a divider or whatever, create a new one, learn more about gardening.

There we go. And then MT six, add a divider or whatever, create a new one, learn more about gardening. There's an idea. Create it. And now we have two ideas. Perfect. Now keep in mind if I were to log out and register a brand new user, if I create Jane's account, of course she's not gonna see John's ideas and we already knew that. But just to, just to give you a little more proof that we have some security in place, let's create an idea for just Jane and we're all set.

Preview Eloquent Relationships12:47

that we have some security in place, let's create an idea for just Jane and we're all set. Okay, so this is looking really good, but, hmm, couple things. Let's go to idea controller. Oh, this works. Idea. Query outta constraint, get the results. It works and it's fine. But you know how we talked about we can do things like this. Give me the authenticated user. Wouldn't it be cool if I could just say, alright,

Give me the authenticated user. Wouldn't it be cool if I could just say, alright, that user give me their ideas and then I can just say ideas like, wouldn't that be awesome? So is that possible? Yes. Can we do it? Absolutely. When are we gonna do it? The next episode.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟