Reputation: 7105
I am beginner in Laravel. I use migrate to create tables in a database, but I don't want to migrate:rollback
to update my table.
I want to apply changes in my old database without loosing current data.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('family');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
Upvotes: 1
Views: 3086
Reputation: 1306
Yes you can do that by running a new migration file for instance in this case
php artisan migrate:make add_family_to_users
And by that, a new migration file will be created and you can add the family column to it together with the properties. Regarding the position, you can use the
->after('column name ')
to specify where the newly created column will precede. For instance, in your case, it would look something similar to this
Schema::table('users', function (Blueprint $table)
{
$table->string('family')->after('name');
});
lastly, re-run php artisan migrate
and check in your table if the column has been added.
Upvotes: 1
Reputation: 1059
As far as I know, this is not possible and not the intention of migrations. I think there are two options to solve this:
If you aren't in production, you can change the migration script and add the additional column manually using mysql:
alter table users add column family varchar(255);
If you need to update a productive database, you should add a second migration script only adding the additional column, e.g.:
public function up()
{
Schema::table('users', function(Blueprint $table)
{
$table->string('family')->after('user_id');
});
}
public function down()
{
Schema::table('users', function(Blueprint $table)
{
$table->dropColumn('family');
});
}
Upvotes: 7