miguelopezv
miguelopezv

Reputation: 860

Create model with resourceful controller

I know I can create a model with controller by using the command php artisan make:model Task -cand I also can create a resourceful controller with php artisan make:controller TasksController -r. Is there a way to create both a model with a resourceful controller?

Upvotes: 2

Views: 5703

Answers (4)

hameed Hamish
hameed Hamish

Reputation: 37

I suggest a simple method which 100% works for me in laravel 7

php artisan make:model ModelName -mr

This command will create a new model with resourceful controller as well as with migration

-m denotes for migrations -r creates resourceful controller and associate it with model

hope this is usefull for you

Upvotes: 1

Amir
Amir

Reputation: 11

example

php artisan make:model Product -c

  -a, --all             Generate a migration, seeder, factory, and resource controller for the model
  -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
      --api             Indicates if the generated controller should be an API controller
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Upvotes: 0

camelCase
camelCase

Reputation: 5598

Yes, you can do this without using packages. If you run php artisan make:model --help you will find the options that you can add to the command.

php artisan make:model --help

Options:
-c, --controller  Create a new controller for the model.
-r, --resource    Indicated if the generated controller should be a resource controller

So if you run it with both the c and the r flag, it will generate the model, along with a resource controller:

php artisan make:model Task -c -r

Note: this works for versions >=5.3!

Upvotes: 5

motia
motia

Reputation: 1999

You may want to look at a generator package.

Upvotes: 1

Related Questions