kim*_*sen 229 php laravel laravel-4 laravel-migrations
我无法弄清楚如何使用Laravel框架将新列添加到现有数据库表中.
我尝试使用...编辑迁移文件
<?php
public function up()
{
Schema::create('users', function ($table) {
$table->integer("paid");
});
}
Run Code Online (Sandbox Code Playgroud)
在终端,我执行php artisan migrate:install和migrate.
如何添加新列?
Phi*_*rks 525
要创建迁移,可以在Artisan CLI上使用migrate:make命令.使用特定名称可避免与现有模型发生冲突
对于Laravel 3:
php artisan migrate:make add_paid_to_users
Run Code Online (Sandbox Code Playgroud)
对于Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用该Schema::table()方法(因为您正在访问现有表,而不是创建新表).你可以添加这样的列:
public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记添加回滚选项:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('paid');
});
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以运行迁移:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
Laravel 3的文档中都详细介绍了这一点:
对于Laravel 4/Laravel 5:
编辑:
用于$table->integer('paid')->after('whichever_column');在特定列之后添加此字段.
cam*_*ase 61
我将使用Laravel 5.1及其后的版本为未来读者添加mike3875的答案.
为了使事情更快,您可以使用标志"--table",如下所示:
php artisan make:migration add_paid_to_users --table="users"
Run Code Online (Sandbox Code Playgroud)
这将自动添加up和down方法内容:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
Run Code Online (Sandbox Code Playgroud)
类似地,您可以--create["table_name"]在创建新迁移时使用该选项,这将为迁移添加更多样板.小点,但在加载它们时很有帮助!
mik*_*yuk 24
如果您使用的是Laravel 5,那么命令就是;
php artisan make:migration add_paid_to_users
Run Code Online (Sandbox Code Playgroud)
所有用于制作的命令(控制器,模型,迁移等)都已在make:命令下移动.
php artisan migrate 虽然仍然是一样的.
che*_*aby 18
通过执行以下命令创建新迁移:make:migration
示例:
php artisan make:migration add_store_id_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)
在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');
});
}
}
Run Code Online (Sandbox Code Playgroud)
之后运行命令:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
如果您因任何原因要撤消上次迁移,请运行以下命令:
php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)
您可以在文档中找到有关迁移的更多信息
tpl*_*ner 17
您可以在初始Schema::create方法中添加新列,如下所示:
Schema::create('users', function($table) {
$table->integer("paied");
$table->string("title");
$table->text("description");
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
如果您已经创建了表,则可以通过创建新迁移并使用该Schema::table方法向该表添加其他列:
Schema::table('users', function($table) {
$table->string("title");
$table->text("description");
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
Laravel 7
使用 cli 命令创建迁移文件:
php artisan make:migration add_paid_to_users_table --table=users
将在迁移文件夹中创建一个文件,在编辑器中打开它。
添加到函数 up():
Schema::table('users', function (Blueprint $table) {
// Create new column
// You probably want to make the new column nullable
$table->integer('paid')->nullable()->after('status');
}
Run Code Online (Sandbox Code Playgroud)
添加到函数 down(),这将在迁移因某些原因失败时运行:
$table->dropColumn('paid');
使用 cli 命令运行迁移:
php artisan migrate
如果您想向表中添加一列以创建外键约束:
在上述过程的第 3 步中,您将使用以下代码:
$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
Run Code Online (Sandbox Code Playgroud)
在上述过程的第 4 步中,您将使用以下代码:
// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
Run Code Online (Sandbox Code Playgroud)
小智 8
this things is worked on laravel 5.1.
first, on your terminal execute this code
php artisan make:migration add_paid_to_users --table=users
Run Code Online (Sandbox Code Playgroud)
after that go to your project directory and expand directory database - migration and edit file add_paid_to_users.php, add this code
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('paid'); //just add this line
});
}
Run Code Online (Sandbox Code Playgroud)
after that go back to your terminal and execute this command
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
hope this help.
小智 8
步骤1
php artisan make:migration add_sex_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)
第2步
在新生成的迁移文件中,你会发现up和down的hook方法。在上钩中添加要添加的列,在下钩中添加需要删除的列。例如,我需要在用户列中添加性别,因此我将在向上挂钩中添加以下行。
$table->integer('quantity')->default(1)->nullable();
Run Code Online (Sandbox Code Playgroud)
所以我有这样的东西
public function up()
{
Schema::table('service_subscriptions', function (Blueprint $table) {
$table->integer('quantity')->default(1)->nullable();
});
}
Run Code Online (Sandbox Code Playgroud)
步骤3
运行迁移命令如下
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
然后您将添加一个新列
小智 7
在 Laravel 8
php artisan make:migration add_columnname_to_tablename_table --table=tablename
Run Code Online (Sandbox Code Playgroud)
然后在创建迁移之后
public function up()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->datatype('column_name')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
// 1. Create new column
$table->dropColumn('column_name');
});
}
Run Code Online (Sandbox Code Playgroud)
然后运行
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
如果您遇到错误,请使用创建表之前的日期重命名迁移名称,然后再次运行 php artisan migrate
小智 6
您只需修改现有的迁移文件,例如在表中添加一列,然后在终端中键入:
$ php artisan migrate:refresh
Run Code Online (Sandbox Code Playgroud)
小智 6
首先回滚之前的迁移
php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)
之后,您可以修改现有的迁移文件(添加新的、重命名或删除列),然后重新运行您的迁移文件
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
316253 次 |
| 最近记录: |