259

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?

Christophvh
  • 12,586
  • 7
  • 48
  • 70
arun
  • 4,595
  • 5
  • 19
  • 39

16 Answers16

619

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

Christophvh
  • 12,586
  • 7
  • 48
  • 70
  • 53
    Now we can use `php artisan make:model Todo -a` to create model, migration, resource controller and `factory` – arun Apr 11 '18 at 12:43
  • why, for example, the method `show` param is the model object when creating with `make:model -mcr` vs `make:controller --resource` the method `show` param is `$id` – Cr1xus Jun 07 '19 at 13:55
  • @Cr1xus Because in the second command you need to specify which Resource model you want to use, in the first command the -r flag knows the correct model because we just made that model. – Christophvh Jun 07 '19 at 14:08
  • when I use this command, why it doesnt creates ModelResource.php in app/http/resources directory ? – tyasird Sep 15 '19 at 12:01
  • 1
    @tyasird Resource means that your controllers will be preloaded with auto-injected model. This does not mean the api-resources – Christophvh Sep 17 '19 at 13:53
57

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.

Md. Shafiqur Rahman
  • 2,878
  • 27
  • 24
Arman H
  • 1,594
  • 18
  • 22
34

You can do it with the following command:

php artisan make:model post -mcr

Brief :

-m, to create migration

-c to create controller

-r to specify the controller has resource

sunil
  • 349
  • 3
  • 3
31

You can make model + migration + controller, all in one line, using this command:

php artisan make:model --migration --controller test

Short version: php artisan make:model -mc test

Output :-

Model created successfully.

Created Migration:2018_03_10_002331_create_tests_table

Controller created successfully.


If you need to perform all CRUD operations in the controller then use this command:

php artisan make:model --migration --controller test --resource  

Short version: php artisan make:model -mc test --resource

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • 1
    please use php artisan make:model --migration --controller --resource Test . – Affan Apr 30 '18 at 05:55
  • bro i create first and then post this . actually i use your given command and add --resource at end and this work please check from you end . I am useing laravel 5.4 . may lower version of laravel not support . @Udhav – Affan Apr 30 '18 at 08:24
  • I installed fresh Laravel, Your suggestion code is working, thank you @Affan :) – Udhav Sarvaiya Apr 30 '18 at 09:14
9
php artisan make:model PurchaseRequest -crm

The Result is

Model created successfully.
Created Migration: 2018_11_11_011541_create_purchase_requests_table
Controller created successfully.

Just use -crm instead of -mcr

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
gedeadisurya
  • 111
  • 1
  • 3
7

php artisan make:model Author -cfmsr

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

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

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

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

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

5

Laravel 5.4 You can use

 php artisan make:model --migration --controller --resource Test

This will create 1) Model 2) controller with default resource function 3) Migration file

And Got Answer

Model created successfully.

Created Migration: 2018_04_30_055346_create_tests_table

Controller created successfully.

Affan
  • 1,132
  • 7
  • 16
4

We can use php artisan make:model Todo -a to create model, migration, resource controller and factory

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
3

You don't need to add --resource flag just type the following and laravel will create the whole desired resources

 php artisan make:controller TodoController --model=todo
3

Instead of using long command like

php artisan make:model <Model Name> --migration --controller --resource

for make migration, model and controller, you may use even shorter as -mcr.

php artisan make:model <Model Name> -mcr

For more MOST USEFUL LARAVEL ARTISAN MAKE COMMANDS LISTS

kaushik
  • 903
  • 7
  • 16
3

If you are using Laravel as an only API add --api option:

php artisan make:model Post -a --api
Oybek Odilov
  • 185
  • 9
2

To make mode, controllers with resources, You can type CMD as follows :

 php artisan make:model Todo -mcr

or you can check by typing

php artisan help make:model

where you can get all the ideas

2

You can use -m -c -r to make migration, model and controller.

php artisan make:model Post -m -c -r
Brane
  • 3,257
  • 2
  • 42
  • 53
1

To make all 3: Model, Controller & Migration Schema of table

write in your console: php artisan make:model NameOfYourModel -mcr

clusterBuddy
  • 1,523
  • 12
  • 39
1

How I was doing it until now:

php artisan make:model Customer
php artisan make:controller CustomersController --resource

Apparently, there’s a quicker way:

php artisan make:controller CustomersController --model=Customer
1

php artisan make:model modelname -mcr to create model. Here -mcr stands for migrations components and resourses

omar jayed
  • 850
  • 5
  • 16