我不知道如何使用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');
});
}
其他回答
我将添加到mike3875的答案,供将来使用Laravel 5.1及后续版本的读者使用。
为了让事情变得更快,你可以像这样使用标志"——table":
php artisan make:migration add_paid_to_users --table="users"
这将自动添加up和down方法内容:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
类似地,您可以在创建新的迁移时使用——create["table_name"]选项,这将为您的迁移添加更多的样板。这是个小问题,但在做大量的事情时很有帮助!
如果您想将新列作为外键添加到现有表。
执行命令:make:migration创建新的迁移
例子:
php artisan make:migration add_store_id_to_users_table --table=users
在database/migrations文件夹中,你有一个新的迁移文件,如下所示:
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');
});
}
}
然后执行命令:
php artisan migrate
如果您因为任何原因想要撤消上次迁移,请运行此命令:
php artisan migrate:rollback
您可以在文档中找到更多关于迁移的信息
在拉拉维尔 8
php artisan make:migration add_paid_to_users_table --table=users
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
在laravel
在现有表中添加新列
php artisan make:migration add_paid_to_users_table
如果您想创建新的迁移,那么执行下面的代码
php artisan make:migration create_users_table --create=users
你可以像这样在初始的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没有太大的变化。
要创建迁移,您可以在Artisan CLI中使用migrate:make命令。使用特定的名称以避免与现有模型冲突
对于拉拉维尔 5+:
php artisan make:migration add_paid_to_users_table --table=users
对于拉拉维尔 3:
php artisan migrate:make add_paid_to_users
然后需要使用Schema::table()方法(因为您正在访问一个现有的表,而不是创建一个新表)。你可以像这样添加一列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
不要忘记添加回滚选项:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
然后你可以运行你的迁移:
php artisan migrate
Laravel 4 / Laravel 5的文档都很好地介绍了这一点:
模式构建器 迁移
对于Laravel 3:
模式构建器 迁移
编辑:
用表- >美元整数(支付)- >后(“whichever_column”);将此字段添加到特定列之后。