我想使用列电子邮件和密码登录。
密码在注册过程中进行哈希处理并保存到数据库 ('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/