在Laravel迁移中将新列添加到现有表 [英] Add new columns to existing table in a migration in Laravel

查看:119
本文介绍了在Laravel迁移中将新列添加到现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在laravel的现有表users中添加一些新列.

I want to add some new columns in my existing table users in laravel.

我已经为此进行了搜索,在进行了这些搜索之后,我已经使用命令php artisan make:migration add_columns_to_users创建了迁移.

I have already googling for that and following those search I have already created migration using the command php artisan make:migration add_columns_to_users.

add_columns_to_users.php

public function up()
{
    Schema::table('users', function($table) {
        $table->string('address');
        $table->string('city');
        $table->string('tribe');
        $table->string('country');
        $table->integer('student_id');
        $table->string('tribe_university_name');
        $table->string('student_program_of_study');
        $table->string('faculty');
        $table->string('level');
    });
}

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('address');
        $table->dropColumn('city');
        $table->dropColumn('tribe');
        $table->dropColumn('country');
        $table->dropColumn('student_id');
        $table->dropColumn('tribe_university_name');
        $table->dropColumn('faculty');
        $table->dropColumn('level');
    });
}

创建后,我运行此命令php artisan migrate.

After creation, I run this command php artisan migrate.

但是出现了相同的错误:

But got the same error:

基本表或视图已存在:1050表'用户'已存在(SQL:创建表users(id int unsigned not null auto_increment主键,name varchar(255)不为null,email varchar(255)不为null,password varchar(255)不为null,remember_token varchar(100)为null,created_at时间戳为null,updated_at时间戳为null)默认字符集utf8整理utf8_unicode_ci)

Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8 collate utf8_unicode_ci)

用户表2014_10_12_000000_create_users_table.php的全名,另一个名称为2019_04_11_074552_add_column_to_users.php

Full name of user table 2014_10_12_000000_create_users_table.php and the other name is 2019_04_11_074552_add_column_to_users.php

如何解决这个问题?

我的主要查询是如何在现有表中添加新列?

推荐答案

如果检查错误跟踪:

基本表或视图已存在:1050表用户"已存在

Base table or view already exists: 1050 Table 'users' already exists

这意味着用户表已经存在,因此在您进行迁移时,它试图创建一个已经在数据库中创建的表.

This means that the users table already exists so when you run your migrations it is trying to create a table that is already created in your database.

注意:不要忘记先备份数据库

Note: Don't forget to backup your database first

删除用户表,还会从迁移表中删除用户条目.

Delete users table from the database also delete users entries from migrations table.

之后,执行migration Artisan命令:php artisan migrate

After, execute the migrate Artisan command:php artisan migrate

现在,您的另一个问题是:如何在现有表格中添加新列?

Now another your Question is: How to add new columns in my existing table?

您必须使用以下命令创建表:

You have to create a table using this command:

php artisan make:migration create_users_table

您得到的输出是这样的:创建的迁移:2019_04_12_070152_create_users_table

The output you got it like this: Created Migration: 2019_04_12_070152_create_users_table

您的迁移结构是这样的:

Your Migration structure is something this:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

现在您要在现有用户表中添加新列

php artisan make:migration add_phone_number_to_users_table --table=users

使用Schema::table()方法(因为您正在访问现有表,而不是创建新表).您可以添加这样的列:

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:

public function up()
{
     Schema::table('users', function (Blueprint $table) {
         $table->string('phonenumber')->after('name'); // use this for field after specific column.
     });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('phonenumber');
    });
}

之后,您可以运行迁移:php artisan migrate

After, you can run your migrations: php artisan migrate

您的新列(phonenumber)现在已添加到现有的用户表中,您可以在数据库中查看该表.

Your new columns(phonenumber) are now added to your existing users table, which you can view in your database.

如果您仍然有疑问,请参见此视频

If you have still any doubt, see this video

这篇关于在Laravel迁移中将新列添加到现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆