如何在 Laravel 的默认用户表中添加新列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32624530/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:21:30  来源:igfitidea点击:

How to add new column in Laravel's default Users table?

laravel

提问by Jerielle

I modified the default laravel's user table and I did that by creating my own migration.

我修改了默认的 laravel 的用户表,并通过创建自己的迁移来做到这一点。

Here's my migration sample:

这是我的迁移示例:

 public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('username', 15)->unique()->after('id');
            $table->string('lastname', 15)->unique()->after('username');
            $table->string('firstname', 15)->unique()->after('lastname');
            $table->string('contact')->nullable()->after('email');
            $table->date('birthday')->after('contact');
            $table->tinyInteger('status')->default(1)->after('birthday');
        });
    }

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

And I removed the column namein the default laravel table so it looks like this:

我删除了name默认 Laravel 表中的列,所以它看起来像这样:

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

I checked the values from my form in the AuthController and I dont have an issue in getting the values. Here's what I added in the AuthController

我在 AuthController 中检查了表单中的值,并且在获取值时没有问题。这是我在 AuthController 中添加的内容

protected $username = 'username'; //choose username instead of email
protected $redirectPath = '/dashboard'; //if successful login
protected $loginPath = 'auth/login'; //if not login
protected $redirectAfterLogout = 'auth/login'; //redirect after login

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'getLogout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'username'  => 'required|min:8|max:16|unique:users',
        'lastname'  => 'required',
        'firstname' => 'required',
        'contact'   => 'required',
        'birthday'  => 'date_format: Y-m-d',
        'email'     => 'required|email|max:255|unique:users',
        'password'  => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{

    //dd($data); -- I can get all the values here

    // I added my own column in this part to save. Is it correct?
    return User::create([
        'username'  =>  $data['username'],
        'lastname'  =>  $data['lastname'],
        'firstname' =>  $data['firstname'],
        'birthday'  =>  $data['birthday'],
        'contact'   =>  $data['contact'],
        'email'     =>  $data['email'],
        'status'    =>  1,
        'password'  =>  bcrypt($data['password']),
    ]);
}

And when I try to save the data I can only save these columns

当我尝试保存数据时,我只能保存这些列

mysql> select * from users \G;
*************************** 1. row ***************************
            id: 1
      username:
      lastname:
     firstname:
         email: [email protected]
       contact: NULL
      birthday: 0000-00-00
        status: 1
      password: yNkmqZaje4MKzheqPZKbhOIGD7ZlqYRfIP6DJZz4zb4gXVNvFsv2W
remember_token: PCYczF2Y9ts97TvbDOLsiXO5hxkekkxysmMYuIdN5MsaIY8TnroEof6d2jVM
    created_at: 2015-09-17 06:56:43
    updated_at: 2015-09-17 07:00:35
1 row in set (0.00 sec)

ERROR:
No query specified

How can I add my new column in the database?

如何在数据库中添加我的新列?

Ok that's all I hope you can help me.

好的,这就是我希望你能帮助我的全部内容。

回答by Ahmed Rakha

You can not edit the users table with this way first, go to the terminal and write this command :

您不能先用这种方式编辑用户表,请转到终端并编写以下命令:

php artisan make:migration add_new_fields_to_users_table

and click enter. After that, go to the migration file and in the add_new_fields_to_users_table.php and write your new columns

然后点击进入。之后,转到迁移文件并在 add_new_fields_to_users_table.php 中写入新列

 $table->string('username', 15)->unique()->after('id');
        $table->string('lastname', 15)->unique()->after('username');
        $table->string('firstname', 15)->unique()->after('lastname');
        $table->string('contact')->nullable()->after('email');
        $table->date('birthday')->after('contact');
        $table->tinyInteger('status')->default(1)->after('birthday');

Then , go to the terminal and do this:

然后,转到终端并执行以下操作:

php artisan migrate

and finally go to the User model and add the name's of your new table to the fillable variable

最后转到 User 模型并将新表的名称添加到可填充变量

protected $fillable = [
    'name', 'email', 'password','username',
    'lastname','firstname','contact','birthday','status'
];

I hope this will help you

我希望这能帮到您

回答by Jerielle

Ok I found the answer. I need to put my own custom column in the fillable content:

好的,我找到了答案。我需要将我自己的自定义列放在可填充内容中:

 protected $fillable = ['username', 'lastname', 'firstname', 'contact',  'email', 'birthday', 'status', 'password'];