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

Creating Post Model0:00

Next up, in Laravel 9, you can force scoped bindings. So, to demonstrate this, we need something to work with. So, with that in mind, let me quickly whip up a model for a Post, and let's include any factory, controller, migration, all of that stuff. Okay, so if we have a look at that migration here, yeah, to keep it quick, we might say a Post consists of a title and a body, and then, of course, a Post belongs to a User. So, we could use foreign, we could even do foreignId 4. Let's demonstrate this, because a lot of people don't know it. If you want, you could structure things like this.

Configuring SQLite Database0:33

Let's demonstrate this, because a lot of people don't know it. If you want, you could structure things like this. And yeah, if you still want, you could add constraints, you could say on delete cascade. That would be an option, if you want. Just a slightly different way to go about it. Okay, so if I were to run php artisan migrate. Oh, whoops, I forgot to set up a DB. Let's go into my .env file, and why don't we use sqlite. And then for the database path or name, let's stick with the default,

Seeding Demo Data0:58

Let's go into my .env file, and why don't we use sqlite. And then for the database path or name, let's stick with the default, which is to put it in a database directory and call it database.sqlite. So, I will create that empty file and then migrate my database. There we go, we're all set. So, if I open this up, here's what we get. And here's my post table, and notice the structure is what we'd expect. Okay, next, let's open up my PostFactory and create some demo data. So, we'll say this->faker->sentence body, and I'm sorry, these should be methods now, shouldn't they?

So, we'll say this faker sentence body, and I'm sorry, these should be methods now, shouldn't they? And then we also want a paragraph. Finally, we'll set up the relationship. And this will just be User and give us some kind of dummy User. Yeah, and I think that should do it. So, now I could say php artisan tinker, and we'll do this inline. We'll say app/models/Post, give me a factory, and how about 10 of them. And that should give us 10 posts associated with 10 different users. Have a look here, give it a refresh, and there you go.

Nested User Post Route2:06

And that should give us 10 Posts associated with 10 different Users. Have a look here, give it a refresh, and there you go. 10 Posts for 10 Users. Okay, so now, what if we wanted to view a Post that was created by an associated User? So, we might do something like this, visit /users, an existing User. You might use the post endpoint to access all Posts created by this User, and only created by that User. And then if you want to take it one step further, ideally, this should display the given Post that was created by the given User. This is where scoping comes into play.

ideally, this should display the given Post that was created by the given User. This is where scoping comes into play. So, you might create a controller called UserPostController at show, but I will do it as a closure here. So, if you think about it, we're going to type hint the associated User and the associated Post. And then, why don't we just return the post here? user/1/post/1. Then, of course, we get the Post with an ID of 1, and notice it is associated with a User with an ID of 1. So, this would naturally work.

and notice it is associated with a User with an ID of 1. So, this would naturally work. But the problem is, why don't we visit the Post with an ID of 9? Well, now, this isn't quite right, is it? Yes, I see the Post with an ID of 9, but notice the URI makes it out as if this Post belongs to the User with an ID of 1, when in fact, the Post belongs to the User with an ID of 9. So, you have to decide if you're okay with this or not. Some apps will make, for example, the first wildcard superfluous, or in other words, it doesn't factor into the underlying database query.

Enabling Scoped Bindings3:37

Some apps will make, for example, the first wildcard superfluous, or in other words, it doesn't factor into the underlying database query. But if you'd prefer to not allow this, then you could use scoped bindings. So, in Laravel 8, I think it was a little vague, a little confusing. But if you would add a custom key to the second wildcard, for example, postId, the addition of that alone would basically turn on scoped bindings, which means if I come back and give this a refresh, I bet it's not going to work, and it doesn't. Call to undefined method User::posts. So, you can see what's happening here.

Call to undefined method User posts. So, you can see what's happening here. It's trying to confirm that this User created this Post, and it's doing that by calling a posts method on User, because that would be the natural relationship. But of course, in this case, we haven't created one. I'll do that right now. User posts, return, and what is the relationship? A User can have many Posts. Okay, so now if we come back and try again,

A User can have many Posts. Okay, so now if we come back and try again, aha, now we get a 404, and this is what we'd expect. The Post with an ID or key of nine does not belong to the User with an ID of one. So, therefore, we get a 404. But if we try to access the Post with an ID of one, which does belong to the User with an ID of one, say that three times fast, we do get the proper results. And that's probably what you want. But again, yeah, I would say it's a little vague.

Forcing Scope Bindings5:05

And that's probably what you want. But again, yeah, I would say it's a little vague. Just because I'm using a custom key here doesn't necessarily signal to me, oh, we are turning on scoped bindings. So if you want, in Laravel 9, you can be more explicit by saying scope bindings. And yeah, I kind of prefer this approach, I think. It makes it that much more clear. So this works, but any other Post that's created by a different User will not work. And yeah, that's the behavior I would expect. All right, Laravel 9, forced scope bindings is now an option.

And yeah, that's the behavior I would expect. All right, Laravel 9, forced scope bindings is now an option.

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