6

I am new to laravel. I am working on laravel version 6. I have created migration. It works nicely the first time, but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate. I tried php artisan migrate --path as well but it does not work. To make it work i have to delete the migration file and create it again. I don't want to use php artisan migrate:fresh.

what should i do to run only one migrations file which has been changed?

Akshay Rathod
  • 1,593
  • 3
  • 10
  • 17
  • try `php artisan migrate:refresh` – tempra Oct 02 '19 at 14:11
  • `migrate:refresh` will delete all table and then run migration again. – Dilip Hirapara Oct 02 '19 at 14:12
  • 1
    Please use `php artisan migrate:rollback` for last created migration file. – Dilip Hirapara Oct 02 '19 at 14:13
  • 1
    so ```php artisan migrate:rollback``` will not drop all the tables? – Akshay Rathod Oct 02 '19 at 14:15
  • 2
    All of this information about `rollback` and `reset` aside, if you want to change a migration that has already been run, the rule of thumb is "don't". Create a new migration that changes the table (add/drop/change column, etc) and run `php artisan migrate` again. Migrations are meant to be a "moving forward" type of thing so you don't have to run rollbacks and risk dataloss. – Tim Lewis Oct 02 '19 at 14:26
  • If you don't like to create a new migration file, [make your own artisan command](https://laravel.com/docs/5.0/commands). – nice_dev Oct 02 '19 at 14:47

8 Answers8

4

When you run php artisan migrate it'll check migration table if there is no new file in the migration folder it'll show you nothing to migrate.

If you want to rollback last migration.

To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:

php artisan migrate:rollback

It'll delete the last created table.

The migrate:reset command will roll back all of your application's migrations:

php artisan migrate:reset

The migrate:fresh command will drop all tables.

php artisan migrate:fresh

php artisan migrate:fresh --seed

more info : document

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • Is it `migrate:fresh` or `migrate:refresh`? Edit: Nevermind; I see it in the newer documentation: https://laravel.com/docs/6.x/migrations – Tim Lewis Oct 02 '19 at 14:16
  • `migrate:refresh` will delete all table and auto-create it again. `migrate:fresh` only delete table. – Dilip Hirapara Oct 02 '19 at 14:18
  • If you want to delete only last created table you must use `migrate:rollback` – Dilip Hirapara Oct 02 '19 at 14:18
  • I don't mind but in new document migration concept is the same.https://laravel.com/docs/6.x/migrations#rolling-back-migrations – Dilip Hirapara Oct 02 '19 at 14:19
  • 1
    Gotcha; I hadn't seen the `fresh` command before, but seems to be a newer addition (Laravel 5.5+) that effectively does the same thing as `refresh`, but doesn't rollback; it just drops everything and does it again. Good to know! – Tim Lewis Oct 02 '19 at 14:19
2

Sadly impossible. The best workaround is to use seeders and use php artisan db:seed after you use php artisan migrate:fresh. Why don't you want to use that?

Norbert Jurga
  • 204
  • 4
  • 14
2

there is two things to do you that you can use 1. In your database there is a table called migrations. Delete the rows from there which one is you want to migrate. That should be there. 2.create a folder inside of database/migrations/folder. And put the migrations file inside the folder then in your command prompt run this below command:

php artisan migrate:refresh --path=database/migrations/folder

option 2 is better than the option 1. I always use this. So i recommend option 2. This should be work

Tanvir Ahmed
  • 969
  • 12
  • 24
2

If you have seeder data then you simply do: php artisan migrate: fresh -- seed . It help to re-migrate your migration with seeder data

Anil Stha
  • 515
  • 7
  • 12
1

You didn't understand well migrations mechanics.

but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate

You write migration to make some changes in database and run it once. If you want to do next updates to database you need next migration.

During development process you can rerun migrations by php artisan migrate:fresh, but on production make sure your migration makes everything you want.

Laravel stores informations about migrations in database in table 'migrations'. If you want to reset some migrations files you can try deleting or edit some records in that table.

colorgreen
  • 265
  • 1
  • 2
  • 12
  • The `migrations` table stores the name (key) of the migration and the batch. There's not much point to manually editing that table when you have the various migration commands to do it for you. It also doesn't have an `id` or `autoincrement` column, so running SQL against it is sometimes a hassle. – Tim Lewis Oct 02 '19 at 14:25
1

Have you checked that the migration has already run or not in the migration table. If its run then there will be a row respective to your migration. If you do changes to an old migration than nothing will be reflected when you run command php artisan migrate, as it has already been migrated.

Saher Siddiqui
  • 363
  • 3
  • 15
1

For every modification on existing (already migrated tables) you will have to make a new migration with modifications only. If you have "users" table migration 2014_10_12_000000_create_users_table like:

 Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');         
        $table->rememberToken();
        $table->timestamps();
    });

and you need to split "name" column, will have to php artisan make:migration alter_table_users --table="users" and add what you want to change:

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('name', 'first_name'); // rename column
    $table->string('last_name'); // add new column  });

Reverse

public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('first_name', 'name');
            $table->dropColumn('last_name');
        });
    }

Nou you can use php artisan migrate

Documentation: https://laravel.com/docs/6.x/migrations#modifying-columns

Militaru
  • 521
  • 5
  • 11
0

No one replied question "Akshay Rathod" but "Militaru" replied exactly what he need php artisan make:migration alter_table_users --table="users" and copy you new fields inside the up() function as under

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('userimage')->nullable();
            $table->string('contact_no')->nullable();
            $table->string('website')->nullable();
            $table->string('country_id')->nullable();
            $table->string('timezone')->nullable();
            $table->string('currency')->nullable();
            $table->string('communication_email')->default(1);
            $table->string('communication_phone')->default(0);
            $table->string('allow_marketing')->default(0);
        });
    }