Laravel single line command for roll back and migration - 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 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

Related

Rollback and Migrate by single command line in 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

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

How can I migrate my new migration file in Laravel?

sorry for silly question but I'm really facing problem. After migrating all the migration files. When I need another features for my project I have to make another table but When I'm going to migrate the new table using "php artisan migrate:rollback" or "php artisan migrate:rollback step=17" .... all the migration files are migrating this time too and I am losing all previous data. Then How can I migrate only the new one?
To migrate only new migrations is simple: php artisan migrate. The way the process works is that Laravel creates a table in your database called migrations. In that table are the names of the migrations that have been run already. If there are new migrations the above command will work.
You can read more about running migrations in the documentation.
To migrate any new migration created php artisan migrate is the artisan command.
To get the details of all commands php artisan is the artisan command.
To get the details of any specific command php artisan help command-name-here.
In the case of creating a new file, you only need to run php artisan migrate again.
If you want modify original migration file and migrate it again, you can use this package to migrate.
First, install package in your Laravel project:
composer require caloskao/migrate-specific
Register command at app/Console/Kernel.php :
protected $commands = [
\CalosKao\MigrateSpecific::class
];
Now, run this command to migrate your file
php artisan migrate:specific database/migrations/table.php

Resources