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

浪子不回头ぞ 提交于 2019-11-29 14:24:48

问题


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. email
  3. password
  4. permissions
  5. last_login
  6. first_name
  7. last_name
  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();

Now when i use 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

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?


回答1:


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',
    ];

}

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',

],


来源:https://stackoverflow.com/questions/26133447/laravel-cartalyst-sentinel-adding-a-username-column-to-users-table-what-is-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!