Laravel Cartalyst Sentinel - 向用户表添加用户名列(正确的方法是什么)

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

Laravel Cartalyst Sentinel - Adding a username column to users table (What is the right way)

phplaravelaclcartalyst-sentinel

提问by arkhamDev

I've pulled in cartalyst/sentinel and i've run the migrations required to generate the tables

我已经引入了 cartalyst/sentinel 并且我已经运行了生成表所需的迁移

php artisan migrate --package=cartalyst/sentinel

I notice that these are the columns available in the users table

我注意到这些是用户表中可用的列

  1. id
  2. email
  3. password
  4. permissions
  5. last_login
  6. first_name
  7. last_name
  8. created_at
  9. updated_at
  1. ID
  2. 电子邮件
  3. 密码
  4. 权限
  5. 上次登录
  6. created_at
  7. 更新时间

I'd like to add username after the email. So i created a migration file that does that.

我想在电子邮件后添加用户名。所以我创建了一个迁移文件来做到这一点。

//add a column username after email in the users table
$table->string('username')->after('email')->unique();

Now when i use Sentinel::register

现在当我使用 Sentinel::register

$credentials = Input::all();
$user = Sentinel::register($credentials);

The username doesn't get saved in the table. So i've managed to get it fillable by editing vendor/cartalyst/sentinel/src/Users/EloquentUser.php

用户名不会保存在表中。所以我设法通过编辑vendor/cartalyst/sentinel/src/Users/EloquentUser.php来填充它

protected $fillable = [
    'email',
    'username', /* i added this */
    'password',
    'last_name',
    'first_name',
    'permissions',
];

Now this works, the username gets stored in the table. But im wondering if what i'm doing is right?Should we not touch the files in the packages folder. How do we solve this?

现在这有效,用户名被存储在表中。但我想知道我所做的是否正确?我们应该不接触包文件夹中的文件吗?我们如何解决这个问题?

回答by Antonio Carlos Ribeiro

Almost. You have to create your own User clas, extending vendor/cartalyst/sentinel/src/Users/EloquentUser.php:

几乎。您必须创建自己的用户类,扩展vendor/cartalyst/sentinel/src/Users/EloquentUser.php

use Cartalyst\Sentinel\Users\EloquentUser as CartalystUser;

class User extends CartalystUser {

    protected $fillable = [
        'email',
        'username', /* i added this */
        'password',
        'last_name',
        'first_name',
        'permissions',
    ];

}

Publish Sentinel's config:

发布 Sentinel 的配置:

php artisan config:publish cartalyst/sentinel

And in the config file, set the user model to your own:

在配置文件中,将用户模型设置为您自己的:

'users' => [

    'model' => 'Your\Namespace\User',

],