php - Laravel attempts() 不起作用,getAuthIdentifier 使用错误的列

标签 php mysql authentication laravel

我想使用列电子邮件和密码登录。 密码在注册过程中进行哈希处理并保存到数据库 ('driver' => 'database')。 电子邮件列不是主键,只是唯一的。

AuthController.php:

// Get all the inputs
        $userdata = array(
            'email' => Input::get('username'),
            'password'  => Input::get('password')
        );

        // Declare the rules for the form validation.
        $rules = array(
            'email' => 'Required',
            'password'  => 'Required'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes())
        {

            // Try to log the user in.
            if (Auth::attempt($userdata, true))
            {
                // Redirect to homepage
                return Redirect::to('')->with('success', 'You have logged in successfully');
            }
            else
            {
                // Redirect to the login page.
                return Redirect::to('login')->withErrors(array('password' => 'password invalid'))->withInput(Input::except('password'));
            }
        }

无论如何,我刚刚收到错误: 错误异常 未定义索引:id

它还向我展示了这一点:

 public function getAuthIdentifier()
        {
            return $this->attributes['id'];
        }

我做错了什么?谢谢

编辑

用户模型:

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {

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

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

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

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

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

最佳答案

getAuthIdentifier 是一个接口(interface)方法。 GenericUser 类正在实现该方法并需要用户 ID。

因此请检查您的模型上是否确实具有 id 属性。

关于php - Laravel attempts() 不起作用,getAuthIdentifier 使用错误的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22128931/

相关文章:

c# - 如何使用 ASP.NET WebForms 和现有的 SQL Server 数据库实现登录功能?

php - 为什么要在 php 的内部函数中添加命名空间?

php - 更改 php 上传 tmp 文件权限。 Clamav 守护进程无法访问

PHP警告: cannot modify header information

java - 如何向依赖于同一注册表的其他两个字段的字段添加新值?

PHP和MySQL选择框

postgresql - 连接到 Heroku PostgreSQL 数据库时的身份验证错误

c# - WebClient中如何获取Cookie的JSESSIONID

php - MYSQL连接两个表

mysql - 选择不使用表格的两个日期之间的所有日期(生成日期列表)