Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.3通过uniqueidentifier返回Auth:user()_Laravel_Authentication_Laravel 5.3_Uniqueidentifier - Fatal编程技术网

Laravel 5.3通过uniqueidentifier返回Auth:user()

Laravel 5.3通过uniqueidentifier返回Auth:user(),laravel,authentication,laravel-5.3,uniqueidentifier,Laravel,Authentication,Laravel 5.3,Uniqueidentifier,虽然花了一段时间,但我发现了如何使用自定义用户表以及自定义用户名和密码字段成功验证Laravel5.3中的用户 我需要更改我的用户模型: protected $table = 'Contact'; protected $primaryKey = 'ContactId'; public function getAuthPassword() { return $this->New_hashedpassword; } protected $table

虽然花了一段时间,但我发现了如何使用自定义用户表以及自定义用户名和密码字段成功验证Laravel5.3中的用户

我需要更改我的用户模型:

protected $table        = 'Contact';    
protected $primaryKey   = 'ContactId';

public function getAuthPassword()
{
    return $this->New_hashedpassword;
}
protected $table        = 'Contact';    
protected $primaryKey   = 'ContactId';
public $incrementing    = false;
LoginController Http\Controllers\Auth\LoginController.php:

public function username()
{
    return 'EMailAddress1';
}
为了进行测试,我还更改了LoginController中的重定向:

protected function redirectTo()
{
    dd(Auth::user());
}
成功登录后,将正确的用户模型传递给浏览器

我现在面临的唯一问题是,这个自定义表使用MSSQL uniqueidentifier作为主键。现在,当我调用Auth::user()->someUserAttribute时,我的Laravel应用程序抛出一个错误:

[SQL Server]Operand type clash: uniqueidentifier is incompatible with int (SQL: select top 1 * from [Contact] where [Contact].[ContactId] = 7164)
出于某种原因,此用户的实际联系人ID(字符串“07164BAE-33AE-E511-AE88-000C29C93884”)被转换为整数,结果为“7164”

我不明白为什么LoginController可以毫无问题地访问Auth::user(),但是应用程序中的任何其他地方访问Auth::user()都会抛出错误

蒂亚, 沃特

编辑

当我按照Jeff的建议编辑用户模型时,通过添加getAuthIdentifier并硬编码测试用户的UUID,可以成功访问Auth::User()对象。但我如何告诉Laravel将ContactId转换为字符串而不是整数

public function getAuthIdentifier()
{
    //return $this->ContactId;      
    return '07164BAE-33AE-E511-AE88-000C29C93884';
}

正在尝试在用户模型中覆盖此方法

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->ContactId;
}
注:此方法是“可验证”特征的一部分


让我知道这是否适用于您。

我找到了答案。诀窍是通过将主键的自动增量添加到用户模型中,消除主键的自动增量,让Laravel知道主键字段的类型为string:

protected $table        = 'Contact';    
protected $primaryKey   = 'ContactId';

public function getAuthPassword()
{
    return $this->New_hashedpassword;
}
protected $table        = 'Contact';    
protected $primaryKey   = 'ContactId';
public $incrementing    = false;

杰夫,这不管用。我还尝试将ContactId显式转换为字符串“return(string)$this->ContactId”,但这也没有帮助。这在Laravel 4.2中使用过,并且使用此“ContactId”作为用户表的主键真的很有必要吗?如果您添加“id”列(而不删除ContactId),该怎么办?不幸的是,这不是一个选项。只是想感谢您发布了答案。(我也有同样的问题。UUID用于主键,以允许对DB进行切分。)