php 向迁移中的现有表添加新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16791613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a new column to existing table in a migration
提问by kim larsen
I can't figure out how to add a new column to my existing database table using the Laravel framework.
我不知道如何使用 Laravel 框架将新列添加到我现有的数据库表中。
I tried to edit the migration file using...
我尝试使用...编辑迁移文件
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
In terminal, I execute php artisan migrate:install
and migrate
.
在终端中,我执行php artisan migrate:install
和migrate
。
How do I add new columns?
如何添加新列?
回答by Phill Sparks
To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models
要创建迁移,您可以在 Artisan CLI 上使用 migrate:make 命令。使用特定名称以避免与现有模型发生冲突
for Laravel 3:
对于 Laravel 3:
php artisan migrate:make add_paid_to_users
for Laravel 5+:
对于 Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
You then need to use the Schema::table()
method (as you're accessing an existing table, not creating a new one). And you can add a column like this:
然后您需要使用该Schema::table()
方法(因为您正在访问现有表,而不是创建新表)。您可以添加这样的列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
and don't forget to add the rollback option:
并且不要忘记添加回滚选项:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
Then you can run your migrations:
然后你可以运行你的迁移:
php artisan migrate
This is all well covered in the documentation for both Laravel 3:
这在 Laravel 3 的文档中都有很好的介绍:
And for Laravel 4 / Laravel 5:
对于 Laravel 4 / Laravel 5:
Edit:
编辑:
use $table->integer('paid')->after('whichever_column');
to add this field after specific column.
用于$table->integer('paid')->after('whichever_column');
在特定列之后添加此字段。
回答by camelCase
I'll add on to mike3875's answer for future readers using Laravel 5.1 and onward.
我将为使用 Laravel 5.1 及更高版本的未来读者添加 mike3875 的答案。
To make things quicker, you can use the flag "--table" like this:
为了使事情更快,您可以像这样使用标志“--table”:
php artisan make:migration add_paid_to_users --table="users"
This will add the up
and down
method content automatically:
这将自动添加up
和down
方法内容:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
Similarily, you can use the --create["table_name"]
option when creating new migrations which will add more boilerplate to your migrations. Small point, but helpful when doing loads of them!
同样,您可以--create["table_name"]
在创建新迁移时使用该选项,这将为您的迁移添加更多样板。小点,但在做大量工作时很有帮助!
回答by chebaby
laravel 5.6 and above
Laravel 5.6 及以上
in case you want to add new column as a FOREIGN KEY to an existing table.
如果您想将新列作为外键添加到现有表中。
Create a new migration by executing this command : make:migration
通过执行以下命令创建一个新的迁移:make:migration
Example :
例子 :
php artisan make:migration add_store_id_to_users_table --table=users
In database/migrations folder you have new migration file, something like :
在 database/migrations 文件夹中,您有新的迁移文件,例如:
2018_08_08_093431_add_store_id_to_users_table.php(see the comments)
2018_08_08_093431_add_store_id_to_users_table.php(见评论)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddStoreIdToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
// You probably want to make the new column nullable
$table->integer('store_id')->unsigned()->nullable()->after('password');
// 2. Create foreign key constraints
$table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Drop foreign key constraints
$table->dropForeign(['store_id']);
// 2. Drop the column
$table->dropColumn('store_id');
});
}
}
After that run the command :
之后运行命令:
php artisan migrate
In case you want to undo the last migration for any reason, run this command :
如果您出于任何原因想要撤消上次迁移,请运行以下命令:
php artisan migrate:rollback
You can find more information about migrations in the docs
您可以在文档中找到有关迁移的更多信息
回答by mikelovelyuk
If you're using Laravel 5, the command would be;
如果您使用的是 Laravel 5,则命令为;
php artisan make:migration add_paid_to_users
All of the commands for making things (controllers, models, migrations etc) have been moved under the make:
command.
所有用于制作事物的命令(控制器、模型、迁移等)都已移至该make:
命令下。
php artisan migrate
is still the same though.
php artisan migrate
不过还是一样。
回答by tplaner
You can add new columns within the initial Schema::create
method like this:
您可以在初始Schema::create
方法中添加新列,如下所示:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
If you have already created a table you can add additional columns to that table by creating a new migration and using the Schema::table
method:
如果您已经创建了一个表,您可以通过创建一个新的迁移并使用以下Schema::table
方法向该表添加其他列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
The documentation is fairly thorough about this, and hasn't changed too much from version 3to version 4.
回答by Mahana Delacour
you can simply modify your existing migration file, for example adding a column in your table, and then in your terminal typing :
您可以简单地修改现有的迁移文件,例如在表中添加一列,然后在终端中输入:
$ php artisan migrate:refresh
回答by Rosidin Bima
this things is worked on laravel 5.1.
这些东西在 Laravel 5.1 上有效。
first, on your terminal execute this code
首先,在您的终端上执行此代码
php artisan make:migration add_paid_to_users --table=users
after that go to your project directory and expand directory database - migration and edit file add_paid_to_users.php, add this code
之后转到您的项目目录并展开目录数据库 - 迁移和编辑文件 add_paid_to_users.php,添加此代码
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
after that go back to your terminal and execute this command
之后返回您的终端并执行此命令
php artisan migrate
hope this help.
希望这有帮助。
回答by noobgrammer
First rollback your previous migration
首先回滚之前的迁移
php artisan migrate:rollback
After that, you can modify your existing migration file (add new , rename or delete columns) then Re-Run your migration file
之后,您可以修改现有的迁移文件(添加新的、重命名或删除列),然后重新运行您的迁移文件
php artisan migrate
回答by mfink
Although a migration file is best practice as others have mentioned, in a pinch you can also add a column with tinker.
尽管正如其他人提到的那样,迁移文件是最佳实践,但在紧要关头,您还可以添加一个包含 tinker 的列。
$ php artisan tinker
Here's an example one-liner for the terminal:
这是终端的单行示例:
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })
(Here it is formatted for readability)
(这里是为了可读性而格式化的)
Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){
$table->integer('paid');
});