Part 5: Generate Models – Laravel 5.4 New Features



In Laravel 5.4 we can use new artisan helpers to generate a matching model with our controllers. In fact if we already have a model, we can tell our controller to link to that model.

To get this functionality we need to do a simple command with the –model flag at the end.

php artisan make:controller ControllerName –model=ModelName

By adding that flag it will tie the controller to a model. If the model doesn’t exist then it will ask us if it wants to create a new one for us. The default here is to create the new model. If you do not want this for some reason, you can reply to the prompt with “no”. This will create an empty model and a boilerplate resourceful controller.

It is important to note here that controllers are resourceful by default. They will automatically pull in requests on applicable methods in the controller and they will also typehint your model objects whenever applicable as well. The importing of your model into the controller is also automatic. This gives us a lot of bonus functionality for “free” with no effort.

If you tie a controller to a model that already exists, you will get all the same functionality as before, but Laravel will recognize the model already exists and not prompt you to make a new one. All linking and “bonus” functionality occurs as normal and the resourceful controller is made.



source

Reply


Build A Site Info