Laravel Cartalyst Sentinel-向用户表添加用户名列(正确的方法是什么) [英] Laravel Cartalyst Sentinel - Adding a username column to users table (What is the right way)

查看:137
本文介绍了Laravel Cartalyst Sentinel-向用户表添加用户名列(正确的方法是什么)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

php artisan migrate --package=cartalyst/sentinel

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

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

  1. id
  2. 电子邮件
  3. 密码
  4. 权限
  5. last_login
  6. 名字
  7. 姓氏
  8. created_at
  9. updated_at

我想在电子邮件后添加用户名.因此,我创建了一个执行该操作的迁移文件.

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();

现在当我使用Sentinel :: register

Now when i use Sentinel::register

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

用户名未保存在表中.因此,我设法通过编辑 vendor/cartalyst/sentinel/src/Users/EloquentUser.php

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

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

现在这可行,用户名将存储在表中.但是我想知道我在做什么对吗?我们是否不应该触摸packages文件夹中的文件.我们该如何解决呢?

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?

推荐答案

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

Almost. You have to create your own User clas, extending 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',
    ];

}

发布Sentinel的配置:

Publish Sentinel's config:

php artisan config:publish cartalyst/sentinel

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

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

'users' => [

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

],

这篇关于Laravel Cartalyst Sentinel-向用户表添加用户名列(正确的方法是什么)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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