Laravel 5 Auth更改表列

时间:2015-09-01 19:56:51

标签: php mysql laravel-5 laravel-5.1

我更改了用户数据库表中的一些字段。

table name: users
primaryKey: user_id
username: user_username
password: user_password
e-mail: user_mail

在Illuminate \ Foundation \ AuthAuthenticatesUsers中我添加了protected $username = 'user_username';

当我尝试登录我的帐户时,在我提供用户名和密码后会看到一个空白页面。调试已启用但无法正常工作。发生了什么事?

Auth::attempt(array(
            'user_username' => 'pionas',
            'user_password'  => '12345',
        ));

User模型中,我添加了getAuthPassword并将列名更改为user_password。记录很清楚。

Auth::loginUsingId(1); - 无法正常工作

可能Auth中的所有方法都无效。

我的用户模型是:

<?php
namespace App\User;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
protected $primaryKey = 'user_id';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
protected $hidden = array('user_password', 'remember_token');

/**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    public function comment()
    {
        return $this->hasOne('UserField', 'user_id');
    }
    /**

2 个答案:

答案 0 :(得分:2)

您似乎已经剥离了Laravel 5.1使用的一些必需特征。以下是已更新的特征的更新用户模型:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';


    protected $primaryKey = 'user_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('user_password', 'remember_token');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->user_mail;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    public function comment()
    {
        return $this->hasOne('UserField', 'user_id');
    }

    /**
     * Scope a query to only include users of a given type.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOfStatus($query, $type)
    {
        return $query->where('user_status', $type);
    }

}

答案 1 :(得分:0)

你需要做两件事。

  1. 在您的模型上,添加方法getAuthPassword()(您已经这样做了)

    public function getAuthPassword()
    {
        return $this->user_password;
    }
    
  2. 在AuthController上,将$凭据传递给$ this-&gt; auth-&gt; validate()方法(或Auth :: attempt()或其他任何东西)时,必须设置数组检查密码的密钥,这个密钥应该命名为“密码”。所有其他键可以有任何名称,您将使用用户名栏的名称

    $credentials['user_username'] = $request->input('user_username');
    $credentials['password'] = $request->input('user_password');
    
    Auth::attempt($credentials);