Laravel Migrate Specific File(s) from Migrations

Viewed 363811

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.

29 Answers

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

if you use tab for autocomplete

php artisan migrate --path='./database/migrations/2019_12_31_115457_create_coworking_personal_memberships_table.php'
php artisan migrate --path=database/migrations/2020_04_10_130703_create_test_table.php

Note :

after --path no / before

You can run command like this

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

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)

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.
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

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

I hope this will help you a lil bit. Happy Coding.

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

If you want to create one and specific table. You can use this code. It works for laravel(5.x) versions.

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

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

Correction- remove slash before the database

$ php artisan migrate --path=database/migrations/migration.php
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.

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

Delete the table and remove its record from migration table.

After that you just run migration again:

php artisan migrate

Specific Table Migration

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

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

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

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

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

You can use:

php artisan migrate:refresh --path=/database/migrations/<migration table here>

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 .

Related