Laravel delete model, controller and migration in single artisan command? - laravel

As the title says; is there a way to delete/revert/rollback the creation of the files created when running php artisan make:model MyModel -mcr?
Something like:
php artisan destroy:model MyModel
.. and it "cascade" delete all related files?

Just do it manually, no command as of this writing
Delete the model first (if you don't) need the model any longer
Delete the migration from ...database/migrations folder
If you have already migrated i.e if you have already run php artisan migrate, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migration
Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.
Works for me, hope it helps!

When you run
php artisan make:model --help
command you must be see
Usage:
make:model [options] [--] <name>
Arguments:
name The name of the class
Options:
-a, --all Generate a migration, 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
-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
-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 outp
ut and 3 for debug
That mean dy default it is not poosible. You must be make your own artisan command for it. Also if you want know about artisan command options and arguments use
php artisan command_name --help

Related

Running php artisan ide:models automatically

I create a command through make:command
php artisan make:command ResetDBCommand
Then I would like to run the two command in handler
php artisan ide:models
php artisan db:seed
However, it's not work for me to fire the two command automatically through the below codes
Artisan::call('db:seed');
$console->writeln('db:seed done.');
Artisan::call('ide:models--force');
$console->writeln('ide:models done.');
Error:
The command "ide:models--force" does not exist.
how can I do that?
The correct command is ide-helper:models you can confirm this if you do:
php artisan help ide:models
You get:
[...]
Usage:
ide-helper:models [options] [--] [<model>...]
which indicates that Laravel does automatically resolve this command when called in the command line. However such resolution mechanism does not exist when calling it programmatically.
Another issue is that --force is not a valid option in ide-helper:models here's what you can do though:
Artisan::call('db:seed');
$console->writeln('db:seed done.');
// Uncomment one of the two
// Artisan::call('ide-helper:models --nowrite'); // Only write metadata in the _ide_helper_models.php file
// Artisan::call('ide-helper:models --write'); // Write metadata on models
// ------
$console->writeln('ide:models done.');
Pick whichever one you prefer accordingly
Tested the above in Laravel 8

What flags can I use to add methods to controller in artisan:make controller?

I am curious about the php artisan make:contorller myController command. I am using Laravel 5.7.* and I know that we can add flags to that command, but I do not know what are those flags; for example, a flag to add all CRUD methods. How many flags are for this make:controller command? What are those flags? All of the flags works for all the laravel versions (version 5)?
php artisan help make:controller ... enjoy
You can use following flags:
--all -a Generate a migration, factory, and resource controller for the model.
--controller -c Create a new controller for the model.
--factory -f Create a new factory for the model.
--force Create the class even if the model already exists.
--migration -m Create a new migration file for the model.
--pivot -p Indicates if the generated model should be a custom intermediate table model.
--resource -r Indicates if the generated controller should be a resource controller.

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?
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!
You may want to look at a generator package.
https://github.com/amranidev/scaffold-interface
https://github.com/InfyOmLabs/laravel-generator
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
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

View Laravel Artisan Command Signature from CLI

I have a custom artisan command with a signature:
protected $signature = 'user:update
{changedFrom : Timestamp to update users from - "yyyy-mm-dd hh:mm:ss"}
{--extras : Whether to update fringe case "extra" users}';
I am always forgetting the options and their format. I hoped I'd be able to view them in the CLI but the best I can do is to list the description of commands with php artisan.
Is there an artisan command to see the signature?
Adding --help to the command should output the available options.
php artisan user:update --help

making model and migration with a single command in laravel 5.2

I've tried this artisan command as I followed a tutorial
php artisan make:model Foo -m
but I get this error:
exception 'RuntimeException' with message 'The "-m" option does not exist.'
why It's not recognizing that?
If It's a wrong way to do It , what the right one?
In short, this is how you do it.
C:\xampp\htdocs\lms>php artisan make:model Test -m
Model created successfully.
Created Migration: 2016_08_29_160434_create_tests_table
It must work. If that does not, do.
C:\xampp\htdocs\lms>composer install.
and that should work.

Resources