Rollback and Migrate by single command line in laravel - laravel

after rollback php artisan migrate:rollback --step 2 then I need to run command php artisan migrate . is there are any single command line for rollback and migrate ? like php artisan migrate:fresh

You may roll back & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will roll back & re-migrate the last five migrations:
php artisan migrate:refresh --step=2

Related

How to delete and recreate db manually in migrations?

In laravel 8 app to init database of my app I use migrations with command
php artisan migrate:refresh --seed
It works ok if my scripts have no errors, but in case of error(say invalid
fields type in reference to other table) after I fixed error I need to delete and recreate db manually
in phpmyadmin?
If there is a way to auto this process somehow?
Thanks!
You can use php artisan migrate:fresh --seed
With this command it will be drop all tables and re-run migrations
migrate:refresh Just reset your tables, not drops.

Laravel single line command for roll back and migration

after rollback php artisan migrate:rollback --step 2 then I need to run command php artisan migrate . is there are any single command line for rollback and migrate ? like php artisan migrate:fresh
You can run below command
php artisan migrate:rollback --step=2
Also you can refer this documentation https://laravel.com/docs/master/migrations#running-migrations
You may roll back & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will roll back & re-migrate the last five migrations:
php artisan migrate:refresh --step=2

How do I migrate:refresh a specific table in my database?

I want to migrate:refresh a specific table called scores. I tried this:
php artisan migrate:refresh --path=/database/migrations/scores
but it says nothing to migrate.
You cannot refresh a single migration if its not the last one, but you can step back in the migrations chain.
Assuming that you have five migrations and your table is at fourth place you can do like this:
php artisan migrate:refresh --step=2
With this command you refresh the last two migrations.
Note that each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
You can use the --path argument but it should be a directory in wich the command searches for migrations not a single migration file.
If you run:
php artisan help migrate:refresh
You can see all the parameters accepted.
yes, you can do this by using Specify the path to the migrations.
Follow these steps:
first of all to create a migration, use the make:migration command:
php artisan make:migration create_scores1_table
php artisan make:migration create_scores2_table
php artisan make:migration create_scores3_table
The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp like this:
2019_09_04_045427_create_scores1_table.php
2019_09_04_045500_create_scores2_table.php
2019_09_04_045518_create_scores3_table.php
Now, you want to refresh a specific table(for example: scores2 table), so you can be used like this:
php artisan migrate:refresh --path=database/migrations/2019_09_04_045500_create_scores2_table.php
It only refresh the scores2 table and not all the rest of the table
Yes you can migrate:refresh selected migrations. You left off the .php extension from your line of code.
It should look like this example
php artisan migrate:refresh --path=/database/migrations/2020_09_28_224702_create_addresses_table.php
So, if scores is your migration file name, add .php
php artisan migrate:refresh --path=/database/migrations/scores

I can not migrate tables again

I deleted all tables in database, but my error is :
table.questions exists
so I can not migrate them, does it depend on something else as well?
You can run:
php artisan migrate:refresh
to rollback all the tables (including the migrations table) and recreate everything again.
It's impossible if you really removed all the tables from database.
Make sure you really deleted all tables including migrations table.
Normally when you want to revert all migrations you should run:
php artisan migrate:reset
If you haven't done this assuming you have are running Laravel 5.5 you can run:
php artisan migrate:fresh
I would advise you to read the whole Migrations documentation to know how they work and how you should create them. In fact you should not remove tables manually and just run:
php artisan migrate:rollback
to rollback migrations previously run or as I already said you can run one one previously mentioned commands to rollback or rollback and run again your migrations.
You get this error because you already have this table in DB. You need to drop a table in down() method of each migration and use php artisan migrate:rollback command to delete last batch of executed migrations.
In 5.5 you also can use php artisan migrate:fresh if you do not drop tables in the down() method.
https://laravel.com/docs/5.5/migrations#rolling-back-migrations
Or you can just manually recreate a DB and run php artisan migrate command.
I found it , I had a middleware in boot for question table and added this:
if(\Schema::hasTable('questions')) {
....
}

Refresh laravel migration for specific table

Can I run php artisan migrate:refresh for specific table?
Or can I refresh specific table migration in general?
I tried this:
php artisan migrate --path=/database/migrations/selected/
But it's not working!
For Specific File run this command:
php artisan migrate:refresh --path="database\migrations\Your_Migration_File_Name_table.php"
Here --file= is for location of your file and migrate:refresh will empty your table data
If you want to empty all table's data from database then run
php artisan migrate:refresh command.
This works for me:
The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options pre-fill the generated migration stub file with the specified table
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=user
Source: https://laravel.com/docs/5.6/migrations

Resources