Php 在迁移中向现有表添加新列
我不知道如何使用Laravel框架将新列添加到现有的数据库表中 我试图使用…编辑迁移文件Php 在迁移中向现有表添加新列,php,laravel,laravel-4,laravel-migrations,Php,Laravel,Laravel 4,Laravel Migrations,我不知道如何使用Laravel框架将新列添加到现有的数据库表中 我试图使用…编辑迁移文件 要创建迁移,可以在Artisan CLI上使用migrate:make命令。使用特定名称以避免与现有模型冲突 对于Laravel 3: php artisan migrate:make add_paid_to_users 对于Laravel 5+: php artisan make:migration add_paid_to_users_table --table=users php artisan m
要创建迁移,可以在Artisan CLI上使用migrate:make命令。使用特定名称以避免与现有模型冲突
对于Laravel 3:
php artisan migrate:make add_paid_to_users
对于Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
php artisan make:migration add_paid_to_users_table --table=users
然后需要使用Schema::table()
方法(当您访问现有表时,而不是创建新表时)。您可以添加如下列:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
php artisan make:migration add_paid_to_users --table="users"
public function up()
{
架构::表('users',函数($table){
$table->integer('paid');
});
}
别忘了添加回滚选项:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
然后,您可以运行迁移:
php artisan迁移
这在Laravel 3的文档中都有详细介绍:
php artisan migrate:make add_paid_to_users
使用
$table->integer('paid')->后面的('u列中的任何一列')
在特定列之后添加此字段。您可以在初始的Schema::create
方法中添加新列,如下所示:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
php artisan make:migration add_paid_to_users --table="users"
如果已经创建了表,则可以通过创建新迁移并使用Schema::table
方法向该表中添加其他列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
文档对此非常详细,从到没有太多更改。如果您使用的是Laravel 5,那么命令将是
php artisan make:migration add_paid_to_users
所有用于制作东西的命令(控制器、模型、迁移等)都已移动到make:
命令下
php artisan migrate
仍然是一样的。我将补充mike3875的答案,供将来使用Laravel 5.1及更高版本的读者阅读
为了使事情更快,您可以像这样使用标志“-table”:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
php artisan make:migration add_paid_to_users --table="users"
这将自动添加向上
和向下
方法内容:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
类似地,您可以在创建新迁移时使用
--create[“table_name”]
选项,这将为迁移添加更多样板文件。这一点很小,但在做大量工作时很有帮助 警告这是一种破坏性行为。如果使用此选项,请确保首先备份数据库
您只需修改现有迁移文件,例如在表中添加列,然后在终端中键入:
$ php artisan migrate:refresh
这是在laravel 5.1上实现的 首先,在终端上执行此代码
php artisan make:migration add_paid_to_users --table=users
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
然后转到项目目录并展开目录数据库-迁移和编辑文件add_paid_to_users.php,添加以下代码
php artisan make:migration add_paid_to_users --table=users
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
之后,返回终端并执行此命令
php artisan migrate
希望对您有所帮助。laravel 5.6及以上版本
如果要将新列作为外键添加到现有表中。
通过执行以下命令创建新迁移:make:migration
示例:
php artisan make:migration add_store_id_to_users_table --table=users
在database/migrations文件夹中,您有一个新的迁移文件,类似于: 2018\u 08\u 08\u 093431\u将存储\u id\u添加到用户\u table.php(见评论)
如果出于任何原因要撤消上次迁移,请运行以下命令:
php artisan migrate
php artisan migrate:rollback
您可以在第一次回滚上一次迁移中找到有关迁移的更多信息
php artisan migrate:rollback
之后,您可以修改现有迁移文件(添加新列、重命名列或删除列),然后重新运行迁移文件
php artisan migrate
尽管迁移文件是其他人提到的最佳实践,但在必要时,您也可以使用tinker添加一列
$ php artisan tinker
以下是一个用于终端的一个班轮示例:
Schema::table('users',function(\illumb\Database\Schema\Blueprint$table){$table->integer('paid');})
(此处为便于阅读而格式化)
Schema::table('users',function(\illumb\Database\Schema\Blueprint$table){
$table->integer('paid');
});
将列添加到迁移文件并运行此命令
php artisan migrate:refresh --path=/database/migrations/your_file_name.php
首先,您必须创建迁移,您可以在laravel artisan CLI上使用migrate:make命令。旧的laravel版本(如laravel 4)可以使用此命令 对于Laravel 4:
php artisan migrate:make add_paid_to_users
对于laravel 5版本
对于Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
php artisan make:migration add_paid_to_users_table --table=users
然后需要使用Schema::table()。您必须添加以下列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
此外,您还可以查看Laravel 7
php artisan make:migration将付费用户添加到用户表--table=users
$table->dropColumn('paid')代码>
php artisan迁移
如果要向表中添加列以创建外键约束: 在上述过程的步骤3中,您将使用以下代码:
$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
在上述过程的步骤4中,您将使用以下代码:
$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
如果你能把你得到的任何错误都包括在内,那将是非常有用的;你期望发生什么;到底发生了什么?好问题。有很多迁移文档,它向您展示了API以及如何第一次创建表。当你开发更多的应用程序并需要修改数据库结构时,一切都失败了。我制作“db:make”来制作一个新的迁移文件。然后我将Schema::table('users',function($table){$table->integer('paid');});我喜欢它。并运行“php artisan migrate”,但出现致命错误:无法在/Applications/XAMPP/xamppfiles/htdocs/adsense/application/migrations/2013_05_28_122527_Users中重新声明类用户。php第3行创建迁移也包含在文档中