Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 create model, controller and migration in single artisan command

I can create a model and resource controller (binded to model) with the following command

php artisan make:controller TodoController --resource --model=Todo

I want to also create a migration with the above command, is it possible?

like image 362
arun Avatar asked Sep 26 '22 18:09

arun


People also ask

What is resource controller in Laravel?

A resource controller is used to create a controller that handles all the http requests stored by your application. The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller.

How to create a model using artisan in Laravel?

Get to know how to create a model, migration, and controller using a single artisan command in the Laravel application In this short snippet, you will learn how to create a Model, Migration, and Controller using single artisan command. The way to achieve this is by passing a flag to the "make:model" command.

How to create model and migration using Artisan command?

In this short snippet, you will learn how to create a Model, Migration, and Controller using single artisan command. The way to achieve this is by passing a flag to the "make:model" command. The command above will create the model as well as the "migration (m)" and "controller (c)" for the "Post" resource.

How to make a todo controller with resources using artisan?

Controller created successfully. We can use php artisan make:model Todo -a to create model, migration, resource controller and factory To make mode, controllers with resources, You can type CMD as follows : You can use -m -c -r to make migration, model and controller.

How to use artisan make in PHP artisan?

We can use php artisan make:model Todo -a to create model, migration, resource controller and factory To make mode, controllers with resources, You can type CMD as follows : You can use -m -c -r to make migration, model and controller. for make migration, model and controller, you may use even shorter as -mcr.


2 Answers

You can do it if you start from the model

php artisan make:model Todo -mcr

if you run php artisan make:model --help you can see all the available options

-m, --migration Create a new migration file for the model.
-c, --controller Create a new controller for the model.
-r, --resource Indicates if the generated controller should be a resource controller

Update

As mentioned in the comments by @arun in newer versions of laravel > 5.6 it is possible to run following command:

php artisan make:model Todo -a

-a, --all Generate a migration, factory, and resource controller for the model

like image 574
Christophvh Avatar answered Oct 19 '22 10:10

Christophvh


Updated

Laravel 6 or Later

Through the model

To Generate a migration, seeder, factory and resource controller for the model

php artisan make:model Todo -a

Or

php artisan make:model Todo -all

Other Options

-c, --controller Create a new controller for the model

-f, --factory Create a new factory for the model

--force Create the class even if the model already exists

-m, --migration Create a new migration file for the model

-s, --seed Create a new seeder file for the model

-p, --pivot Indicates if the generated model should be a custom intermediate table model

-r, --resource Indicates if the generated controller should be a resource controller

For More Help

php artisan make:model Todo -help

Hope Newbies will get help.

like image 46
Arman H Avatar answered Oct 19 '22 12:10

Arman H