Model Relationships Overview0:06
So let's open our idea model. And if you think about it, there's a relationship between an idea and a user. The idea belongs to the user. And what about the inverse? A user can have many ideas. They could create one or they can have 30 ideas. Alright, so idea belongs to user. And user can have many ideas. That makes sense in my head, right? So we can represent this programmatically
That makes sense in my head, right? So we can represent this programmatically in our code like this. If I have an idea instance and I wanna grab the user who created that idea, I should be able to do it just like this. And here's how we do that. With eloquent, I define the relationship. So an idea belongs to user. There's my method name
Defining Idea BelongsTo0:44
So an idea belongs to user. There's my method name and then I'm gonna represent that relationship type in this case we said belongs to. So we're gonna call belongs to and then reference the eloquent class. That is the related model. Cool. So if we were to test this out in the terminal, you're gonna love this BHB, artisan, tinker, and let's find the first idea in our database.
you're gonna love this BHB, artisan, tinker, and let's find the first idea in our database. So models idea, gimme the first one, whatever it is. All right. Now I can say idea user, and this gives me the user instance. The user who created that specific idea. Now this can be a little confusing at first because we defined user as a method, but when we interact with it, we, we access it as a property.
but when we interact with it, we, we access it as a property. So you'll see if we instead say idea user, we're gonna get a belongs to relationship. And, and you can sometimes reach for that to perform queries. But generally if you want to access a record, you would use the property syntax. And behind the behind the scenes, Laravel is handling that. It has a magic method in place.
And behind the behind the scenes, Laravel is handling that. It has a magic method in place. It's checking what you called so that it can allow for this, uh, API, which is super cool. Alright, so now on any idea model, I can access the user who created it like so, and behind the scenes that's going to perform a database query select, uh, star from user where ID equals two. That's exactly what's going on behind the scenes.
Defining User HasMany2:10
ID equals two. That's exactly what's going on behind the scenes. Okay, so now what about the inverse? If I have a user instance and I wanna access their ideas, then once again I might do something like this. User give me their ideas. Alright, I have my method name ideas, and now what is that relationship type? A user can have many ideas and AI spoil in it for me here,
and now what is that relationship type? A user can have many ideas and AI spoil in it for me here, but you could have guessed that on your own right. A user has many ideas. Very cool. And by the way, if you wanna add return types, you would use the has many return types. These are not required, but many people, um, like them including myself. So if you wanna play along, you can add those as well, but it'll work without them.
So if you wanna play along, you can add those as well, but it'll work without them. Cool. So let's give this one a shot now, PHB, Artis and Tinker. And let's find the first user app models user and let's see if they have any ideas, user ideas. So now this is very important. User ideas could return one or many ideas, right? So we get a collection of items and it looks like this collection has
So we get a collection of items and it looks like this collection has two ideas at the moment. So cool. So that means if you wanna grab for example, this first idea, then you would say user ideas and we can interact with it like an array. So I can do this and now I have my idea instance and I can grab the description like, so important to know. Okay, so let's update some of our code to reflect these new relationships.
Refactoring Controller Queries3:42
Okay, so let's update some of our code to reflect these new relationships. If I return to idea controller right up here where we access all of the ideas that belong to the user, I don't have to do that anymore. Now I can just say, give me the authenticated user and their ideas. And at this point this is kind of superfluous, I think. So I could inline this entirely. You gotta love it. It's so cool. Now while we're here,
Improving Editor Type Hints4:04
So I could inline this entirely. You gotta love it. It's so cool. Now while we're here, a quick little editor tip. Uh, very likely, no matter what editor you're using, you might see something like this, a squiggly line, and then it says, Hey, what's going on here? You're accessing this property, but on your user model, I don't see any reference to an idea's property. So here's what we can do At the very top, we can declare it
to an idea's property. So here's what we can do At the very top, we can declare it as a property or what I can do is if I'm using the Laravel idea plugin, I can say Laravel idea generate helper code. And this is gonna generate a bunch of stuff behind the scenes that makes the editor, uh, basically smarter. So if I were to return to Idea, now you can see that goes away.
So if I were to return to Idea, now you can see that goes away. And if I actually click through here, uh, it's smart enough to know what this is pointing to. So this is just one reason I'm not trying to sell you on Peach B Storm and the Laravel idea plugin, but it's just one reason why I find myself sticking to it. Um, it's so good. Now, alternatively, if you're using a different editor entirely, like Sublime or Rev Obvious code
Now, alternatively, if you're using a different editor entirely, like Sublime or Rev Obvious code or Cursor, another option would be to explicitly declare property reads on your class. So you could do something like this and I'll just paste it in. Uh, so we're gonna have a property here for ideas, and that is a collection. I will import that and that's a collection
I will import that and that's a collection where every key in the collection is just, um, an integer, the index. But all of the values are instances of idea. And in this case, I can simplify that and that would work as well. So if you were to add that and switch back, you'll see your squiggly line go away. But nonetheless, this is so much better, isn't it?
Creating via Relationship5:41
and switch back, you'll see your squiggly line go away. But nonetheless, this is so much better, isn't it? I really love it. Okay, so now let's do one other thing. What about when I store a new idea? Well, I'm still having to reference the authenticated user right here. Instead, maybe I could say something like off user ideas create. That would be kind of cool as well. So can I do that? Yes, of course I can do that.
That would be kind of cool as well. So can I do that? Yes, of course I can do that. So let's update this off user ideas and we're gonna create a new one. So now I don't have to set the user ID 'cause that's gonna happen automatically behind the scenes. And what is ideas? Well, that's the relationship we defined here. And notice in this case, I'm not trying to access a collection of ideas, I'm trying
And notice in this case, I'm not trying to access a collection of ideas, I'm trying to build up a new query. So I call it as a method in this case. And that can be a little confusing at worse, uh, at first. So take, take some time and try to memorize it. If you need any help, of course ask a question in the comments below for the authenticated user, get their ideas and create a new one. It reads so well. So now if I were to switch back,
Authorization Security Issue6:47
and create a new one. It reads so well. So now if I were to switch back, let's, uh, create a new one. Just do gibberish for the time being and that still works just as it did before. Okay, so now I'm gonna leave you with a cliffhanger for the next episode. We have a major security problem at the moment. Yes, Jane can see all of her ideas. That looks good. We can see that the UI is ideas slash four,
Yes, Jane can see all of her ideas. That looks good. We can see that the UI is ideas slash four, but what if we just manually changed it to one, oh, build a house. Did Jane write this? No, but Jane can view an idea that she didn't create. That's a really big problem. So in the next episode, we're gonna diagnose how did this happen and then we're gonna work on the proper authorization
how did this happen and then we're gonna work on the proper authorization to prevent it from happening again. I'll see you then.
