更新时间:2021-12-26 07:12:41
您需要按照以下步骤使用laravel迁移在现有表中添加列.
You need to follow the following steps for adding a column in existing table using laravel migrations.
Laravel 3 +,4 +
使用CLI进行迁移(使用特定名称以避免与现有模型冲突)
Make a migration using CLI as (Use a specific name to avoid ***ing with existing models)
php artisan migrate:make add_dob_users_table --table=users
Laravel 5 +
使用CLI进行迁移(使用特定名称以避免与现有模型冲突)
Make a migration using CLI as (Use a specific name to avoid ***ing with existing models)
php artisan make:migration add_dob_to_users
Laravel 3 +
使用Schema::table()
方法(在访问现有表时,不创建新表),并添加如下列:
Use the Schema::table()
method (As you're accessing an existing table, not creating a new one) and add a column like this:
public function up()
{
Schema::table('users', function($table) {
$table->string('dob')->nullable()->change();
});
}
添加回滚选项:
Add the rollback option:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('dob');
});
}
Laravel 4 +,5 +
使用Schema::table()
方法(在访问现有表时,不创建新表),并添加如下列:
Use the Schema::table()
method (As you're accessing an existing table, not creating a new one) and add a column like this:
public function up()
{
Schema::table('users', function(Blueprint $table) {
$table->string('dob')->nullable()->change();
});
}
添加回滚选项:
Add the rollback option:
public function down()
{
Schema::table('users', function(Blueprint $table) {
$table->dropColumn('dob');
});
}
Laravel 3 +,4 +,5 +
最终运行迁移为
Finally run your migrations as
php artisan migrate
用于创建和运行迁移的文档是
Docs for creating and running migrations are
Laravel 3 +
Laravel 4、5