150

Hi read all the included documentation here in https://laravel.com/docs/5.4/migrations.

Is there a way on how to migrate a certain migration file (1 migration only), cause right now every time there is a change I use php artisan migrate:refresh and all fields are getting reset.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Martney Acha
  • 2,802
  • 4
  • 33
  • 48
  • PSA: Please stop adding answers that are just slight variations on `php artisan migrate:refresh --path=/database/migrations/fileName.php` – General Grievance Mar 31 '23 at 13:36
  • The answer to the question asked is elsewhere, but the real problem is use of `artisan migrate:refresh` which is designed to wipe out the database and start again. It is **not** part of normal application maintenance like `artisan migrate` is. – miken32 Apr 04 '23 at 02:10

27 Answers27

206

First you should create one migration file for your table like:

public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->string('fname',255);
            $table->string('lname',255);
            $table->rememberToken();
            $table->timestamps();
        });
    }

After create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:

php artisan migrate --path=/database/migrations/test/
Mel
  • 5,837
  • 10
  • 37
  • 42
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
188

you should add the path to your migration file to refresh just this table and run

php artisan migrate:refresh --path=/database/migrations/fileName.php
Wissem SASSI
  • 2,181
  • 2
  • 9
  • 13
64

Just look at the migrations table in your database, there will be a list of migration file name and batch number value.

Suppose you have following structure,

id     migration                                           batch

1      2014_10_12_000000_create_users_table                  1 
2      2014_10_12_100000_create_password_resets_table        1
3      2016_09_07_103432_create_tabel_roles                  1

If you want to just rollback 2016_09_07_103432_create_tabel_roles migration, change it's migration batch value to 2 which is highest among all and then just execute following.

php artisan migrate:rollback

Here only table with batch value 2 will be rolled back. Now, make changes to that table and run following console command.

php artisan migrate

Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.

Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single table rollback doesn't violates the integrity among the tables.

Hope you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
34

if you use tab for autocomplete

php artisan migrate --path='./database/migrations/2019_12_31_115457_create_coworking_personal_memberships_table.php'
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
Dawam Raja
  • 437
  • 4
  • 3
24
php artisan migrate --path=database/migrations/2020_04_10_130703_create_test_table.php

Note : after --path no / before

General Grievance
  • 4,555
  • 31
  • 31
  • 45
fahdshaykh
  • 472
  • 3
  • 10
16

You can run command like this

php artisan migrate --path=/database/migrations/2020_04_10_130703_create_test_table.php
Tosca
  • 423
  • 7
  • 12
13

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.
Tuhin
  • 332
  • 3
  • 7
12

You need to put the file(s) into a new directory (ex:selected) and then apply

php artisan migrate  --path=/database/migrations/selected

if you need rollback:

php artisan migrate:rollback  --path=/database/migrations/selected

Note:

php artisan migrate:refresh

this will rollback and then migrate all the migrations files in the default directory (/database/migrations)

Ahmad Zahabi
  • 1,110
  • 12
  • 15
  • No need to create a new directory you can put directly the file name like '--path=/database/migrations/fileName.php' . – Wissem SASSI Dec 12 '19 at 22:57
  • as of laravel 5.6, glob() functions where used which I think only works with directory which makes this answer also correct in a way. – Stanley Aloh Feb 26 '22 at 21:59
12

You can only run this command in your terminal

php artisan migrate --path=database/migrations/2020_10_01_164611_create_asset_info_table.php

After migrations you should put the particular file name. or if you have any folder inside migration then just add that folder name after the migration.

Like this

php artisan migrate --path=database/migrations/yourfolder/2020_10_01_164611_create_asset_info_table.php
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sayandeep Majumdar
  • 370
  • 1
  • 9
  • 20
9
php artisan help migrate

You will see the option:

--path[=PATH] The path to the migrations files to be executed

By the way, you can probably indicate the root folder of the file that you want to migrate:

php artisan migrate --path=/database/migrations/sample.php

Or, You can create a new folder in migrations, then migrate all the migration files you want inside it:

php artisan migrate --path=/database/migrations/new_folder
9

Just use the --path flag. You don't need to use rollback, refresh, or any other command. Example:

php artisan migrate --path=/database/migrations/migration_file_name.php
ouflak
  • 2,458
  • 10
  • 44
  • 49
stephen strange
  • 101
  • 1
  • 2
8

Get the actual file name and path of the migration and run the command like below

php artisan migrate --path=/database/migrations/2021_10_03_071450_create_reset_codes_table.php
Junior
  • 1,007
  • 4
  • 16
  • 26
6

Just wanted to post another solution, which i think is worth mentioning.

  1. Find row with your migration name in migrations table and DELETE it. It should look like this: 2016_06_01_000001_create_oauth_auth_codes_table
  2. Remove your table from database e.g. DROP TABLE oauth_auth_codes
  3. Run php artisan migrate

It will migrate only the table you need, and won't touch anything else

tylik
  • 1,018
  • 1
  • 11
  • 21
6

Correction- remove slash before the database

$ php artisan migrate --path=database/migrations/migration.php
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TCruz
  • 69
  • 1
  • 2
6
php artisan migrate --path=/database/migrations/fileName.php

You don't have to refresh for migration, because refresh means : Rollback all migrations and run them all again.

Atika
  • 1,025
  • 2
  • 6
  • 17
Christian Soto
  • 2,540
  • 2
  • 15
  • 33
5

Specific Table Migration

php artisan migrate --path=/database/migrations/fileName.php
Vishal Vaghasiya
  • 1,857
  • 2
  • 14
  • 21
4

You can only rollback:

php artisan migrate:rollback

https://laravel.com/docs/5.4/migrations#rolling-back-migrations

You can specify how many migrations to roll back to using the 'step' option:

php artisan migrate:rollback --step=1

Some tricks are available here:

Rollback one specific migration in Laravel

Jed
  • 206
  • 1
  • 5
4

Delete the table and remove its record from migration table.

After that you just run migration again:

php artisan migrate
sskoko
  • 819
  • 6
  • 18
3

Or you can simply delete migration file name from your database, in "migrations" table and then run : php artitsan migration

3

You can use:

php artisan migrate:refresh --path=/database/migrations/<migration file here>
Two
  • 512
  • 4
  • 17
2

If you want to create another table, just create a new migration file. It'll will work.

If you create a migration named users_table with id, first_name, last_name. You can create a migration file like

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name',255);
            $table->string('last_name',255);
            $table->rememberToken();
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('users');
    }

If you want to add another field like "status" without migrate:refresh. You can create another migration file like "add_status_filed_to_users_table"

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('status');
    });
} 

And don't forget to add the rollback option:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('status');
    });
}

And when you run the migration with php artisan migration, just migrate the new migration file.

But if you add field "status" into the first migration file (users_table) and run migration. It's nothing to migrate. You need to run php artisan migrate:refresh.

Hope this help.

Junior
  • 1,007
  • 4
  • 16
  • 26
Nguyen Hoang
  • 540
  • 5
  • 25
2

Use: --path=database/migrations/your_migration_file_name.php

Examples:

php artisan migrate:refresh --path=database/migrations/your_migration_file_name.php

php artisan migrate:rollback --path=database/migrations/your_migration_file_name.php

php artisan migrate --path=database/migrations/your_migration_file_name.php

References: Genarate laravel migration

Erielama
  • 111
  • 1
  • 3
1

You could try to use the --path= option to define the specific sub-folder you're wanting to execute and place specific migrations in there.

Alternatively you would need to remove reference and tables from the DB and migrations tables which isn't ideal :/

Chris WB
  • 13
  • 1
  • 3
1

install this package

https://github.com/nilpahar/custom-migration/

and run this command.

php artisan migrate:custom -f migration_name
Ali Hassan
  • 779
  • 10
  • 14
0
php artisan migrate --path=/database/migrations/fileName.php

Just follow the instruction execute this commant file name here should be your migration table name Example:

php artisan migrate --path=/database/migrations/2020_02_21_101937_create_jobs_table.php
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mithun Rana
  • 1,334
  • 1
  • 9
  • 10
-1

First you should to make the following commands:

Step 1:

php artisan migrate:rollback

Step 2:

php artisan migrate

Your table will be back in database .

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • It would be more helpful if you put small description to each step. This will help in understanding the user what actually is happening. This looks like a cheat-code which magically make things work, but outcome might be disastorous in some cases. – Ashwani Agarwal May 03 '20 at 05:09
  • Please read question carefully, the person asked for only one table, your method will rollback all migrations and then create all tables. Also `php artisan migration:refresh` serves the same. – Wajahat Hashmi Jun 11 '20 at 19:00
-1

you can also migrate again migrated table but first you need to go in database migration table and delete row that specific migration name. and then hit php artisan migrate

fahdshaykh
  • 472
  • 3
  • 10