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

Generating Controller With Model0:00

Take a look at this. If we run php artisan and view the help for make:controller, you'll see that there's a new "-m" option. Generate a resource controller for the given model. Let's give it a shot. Let's make a controller for our Post, and I'm going to specify that the associated model is a Post. Okay, so when we do this, if we take a look, you'll see that yes, we get a PostController, and yes, we have all of the resourceful methods, but also, where relevant, you'll see that Post has now been type-hinted.

Testing Route Model Binding0:25

and yes, we have all of the resourceful methods, but also, where relevant, you'll see that Post has now been type-hinted. Now, this alone is very convenient, but as you may know, if we take advantage of route model binding, that means when you call the edit method, you'll instantly receive a Post. Let's try it out. We're going to return the post, and then set up a route for this. How about, when you receive a GET request to /post/{post}/edit, we will hit PostController at edit. Okay, let's view this in the browser. 5.4.dev/post/1/edit, and there we go.

How Binding Works0:58

Okay, let's view this in the browser. 5.4.dev/post/1/edit, and there we go. Because Laravel has route model binding out of the box, it sees this wildcard, it then notes that the variable name here matches the wildcard, so it tracks down the associated Post that has that ID. And that's all there is to it. So from now on, when you require a resourceful controller, and you don't want to manually do this, and update the doc block, and import the associated class, well, just run php artisan make:controller, and then add the --model flag.

Using the --model Flag1:29

makeController, and then add the model flag.

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