Laravel使用不同的列名重置密码

时间:2015-01-03 17:46:29

标签: php laravel laravel-4

我设法让它在授权时工作,使用不同的列命名用户名,电子邮件或密码。

How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication

然而密码提醒似乎无效。

我已将用户模型更改为我的表列名称。

用户模型:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $primaryKey = 'user_id';
    protected $table = 'user';

    public function getReminderEmail() {
        return $this->user_email;
    }

    public function getAuthPassword() {
        return $this->user_pwd;
    }

    public function getRememberTokenName() {
        return 'user_token';
    }

}

用户控制器

Auth::attempt(  array(
 'user_name'    => $username, 
 'password'     => $password
), TRUE );

提醒控制器//错误:找不到列电子邮件 (不确定,它没有读取用户模型 getReminderEmail()

public function post_reminder() {

        switch ($response = Password::remind(Input::only('email'))) {

            case Password::INVALID_USER:
                return Redirect::back()->with('error', Lang::get($response));

            case Password::REMINDER_SENT:
                return Redirect::back()->with('status', Lang::get($response));
        }


}

2 个答案:

答案 0 :(得分:4)

花了我几个小时才搞清楚。 Laravel官方似乎没有提供适当的文档来实现自定义名称字段。

我们只需要更改输入字段名称与表列名称相同。

Field could be anything
<input type="email" name="user_email">

答案 1 :(得分:0)

我认为我们可以添加自定义字段,以下是我想要添加自定义字段的步骤。

1 - 在注册视图中添加input字段。

i.e. <input type="email" name="you_custome_field">

2 - 打开app / services / Registrar.php并在create function.i.e中注册你自定义。

public function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'you_custome_field' => $data['you_custome_field'],
        ]);
    }

3 - (可选)打开user model app / User.php,根据需要将您的字段注册为guardedfillable

我希望这会奏效。