我不知道如何使用Laravel框架向现有的数据库表中添加新列。
我试图编辑迁移文件使用…
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
在终端中,我执行了php artisan migrate:install和migrate。
如何添加新列?
我不知道如何使用Laravel框架向现有的数据库表中添加新列。
我试图编辑迁移文件使用…
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
在终端中,我执行了php artisan migrate:install和migrate。
如何添加新列?
当前回答
首先你必须创建一个迁移,你可以在laravel artisan CLI上使用migrate:make命令。旧的laravel版本,比如laravel 4,你可以使用这个命令 Laravel 4:
php artisan migrate:make add_paid_to_users
和laravel 5版本
对于拉拉维尔 5+:
php artisan make:migration add_paid_to_users_table --table=users
然后需要使用Schema::table()。你需要添加列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
其他回答
你可以像这样在初始的Schema::create方法中添加新列:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
如果您已经创建了一个表,您可以通过创建一个新的迁移并使用Schema::table方法向该表添加额外的列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
关于这一点,文档相当详尽,从版本3到版本4没有太大的变化。
首先回滚之前的迁移
php artisan migrate:rollback
之后,您可以修改现有的迁移文件(添加新列、重命名列或删除列),然后重新运行迁移文件
php artisan migrate
执行如下命令: PHP工匠迁移:新鲜——种子 它将删除表并重新添加它,更新添加到数据库的所有列
向迁移文件中添加列并运行此命令。
php artisan migrate:refresh --path=/database/migrations/your_file_name.php
警告:这是一个破坏性的行动。如果使用这种方法,请确保首先备份数据库。
你可以简单地修改你现有的迁移文件,例如在你的表中添加一个列,然后在你的终端输入:
$ php artisan migrate:refresh