Laravel 5.3 通过 uniqueidentifier 返回 Auth:user() [英] Laravel 5.3 return Auth:user() by uniqueidentifier

查看:49
本文介绍了Laravel 5.3 通过 uniqueidentifier 返回 Auth:user()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了一些时间,但我想出了如何使用自定义用户表以及自定义用户名和密码字段在 Laravel 5.3 中成功验证用户身份.

It took a while, but I figured out how to succesfully authenticate a user in Laravel 5.3 using a custom user table, with custom username and password fields.

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

I needed to alter my User model:

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

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

LoginController Http\Controllers\Auth\LoginController.php:

The LoginController Http\Controllers\Auth\LoginController.php:

public function username()
{
    return 'EMailAddress1';
}

为了测试,我还更改了 LoginController 中的重定向:

For testing, I also changed the redirection in the LoginController:

protected function redirectTo()
{
    dd(Auth::user());
}

成功登录后,正确的 User 模型会传递给浏览器.

After a successful login, the correct User model is passed to the browser.

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

The only problem I face now, is that this custom table uses a MSSQL uniqueidentifier as primary key. So now that when I call Auth::user()->someUserAttribute, my Laravel app throws an error:

[SQL Server]Operand type clash: uniqueidentifier is incompatible with int (SQL: select top 1 * from [Contact] where [Contact].[ContactId] = 7164)

出于某种原因,该用户的实际 ContactID(字符串07164BAE-33AE-E511-AE88-000C29C93884")被转换为 int,结果为7164".

For some reason, the actual ContactID for this user (which is a string "07164BAE-33AE-E511-AE88-000C29C93884") is converted to an int resulting in "7164".

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

I do not understand why the LoginController can access the Auth::user() without any problem, but anywhere else in the application accessing Auth::user() throws an error.

TIA,沃特

编辑

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

When I edit my User model as Jeff suggests, by adding the getAuthIdentifier and hardcoding my test user's UUID, the Auth::user() object can be accessed successfully. But how do I tell Laravel to convert to ContactId to a string instead of an integer?

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

推荐答案

我找到了答案.诀窍是通过将其添加到 User 模型来禁用主键的自动增量,让 Laravel 知道主键字段是字符串类型:

I found the answer. The trick is to let Laravel know the primary key field is of type string by disabeling the auto increment of the primary key by adding this to the User model:

protected $table        = 'Contact';    
protected $primaryKey   = 'ContactId';
public $incrementing    = false;

这篇关于Laravel 5.3 通过 uniqueidentifier 返回 Auth:user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆