Reputation: 1683
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?
Upvotes: 6
Views: 20284
Reputation: 55
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);
});
}
Upvotes: 1
Reputation: 581
If you have seeder data then you simply do: php artisan migrate: fresh -- seed . It help to re-migrate your migration with seeder data
Upvotes: 2
Reputation: 581
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
Upvotes: 2
Reputation: 363
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.
Upvotes: 1
Reputation: 1019
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
Upvotes: 2
Reputation: 315
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.
Upvotes: 1
Reputation: 15316
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
Upvotes: 4
Reputation: 214
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?
Upvotes: 2