将 laravel remember_token 字段更改为其他内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23704367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing laravel remember_token field to something else
提问by Sven van den Boogaart
For my project i use the Auth login, everything works fine until i try to logout with :
对于我的项目,我使用 Auth 登录,一切正常,直到我尝试注销:
Auth::logout();
I use a custom fieldname herrinerToken instead of the default remember_token. In my model/user.php i edited the function getRememberToken() to:
我使用自定义字段名 herrinerToken 而不是默认的 remember_token。在我的模型/user.php 中,我将函数 getRememberToken() 编辑为:
public function getRememberTokenName()
{
return 'herrinerToken';
}
when i try to logout now i get the message:
当我现在尝试注销时,我收到以下消息:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'remember_token' in 'field list' (SQL: update gebruikers
set herrinerToken
= a3eYy1IIbX1FfPhPgmYNTNLwkE7A43vgqwpSU2B5b3EFNHl0ayYF1vUSGCbc, remember_token
= a3eYy1IIbX1FfPhPgmYNTNLwkE7A43vgqwpSU2B5b3EFNHl0ayYF1vUSGCbc where id
= 6)
SQLSTATE [42S22]:未发现柱:1054未知列'remember_token'在'字段列表'(SQL:更新gebruikers
组herrinerToken
= a3eYy1IIbX1FfPhPgmYNTNLwkE7A43vgqwpSU2B5b3EFNHl0ayYF1vUSGCbc,remember_token
= a3eYy1IIbX1FfPhPgmYNTNLwkE7A43vgqwpSU2B5b3EFNHl0ayYF1vUSGCbc其中id
= 6)
So it looks like it tries tu update both remember_token and herrinerToken but i only want to update the herinner_token field. What do i need to adjust to only update the herrinerToken field and not the remember_token field ?
所以看起来它尝试更新 remember_token 和 herrinerToken 但我只想更新 herinner_token 字段。我需要调整什么才能只更新 herrinerToken 字段而不是 remember_token 字段?
回答by menjaraz
Add a herrinerToken
column instead of remember_token
column to your users (or equivalent) database table.
向您的用户(或等效)数据库表中添加一herrinerToken
列而不是remember_token
列。
You should use along with that the following snippet:
您应该同时使用以下代码段:
public function getRememberToken()
{
return $this->herrinerToken;
}
public function setRememberToken($value)
{
$this->herrinerToken = $value;
}
public function getRememberTokenName()
{
return 'herrinerToken';
}
回答by Laurence
I looked into this. It turns out that the field name 'remember_token' is actually hard coded into the DatabaseUserProvider
- so even if you change it in your model - Laravel will still look for 'remember_token' if you are using the Database Auth Driver.
我调查了这个。事实证明,字段名称 'remember_token' 实际上是硬编码到DatabaseUserProvider
- 因此即使您在模型中更改它 - 如果您使用数据库身份验证驱动程序,Laravel 仍会查找 'remember_token'。
You need to switch to the Eloquent auth driver. It seems as though you are using Eloquent for your user model anyway - so there should be now issue switching.
您需要切换到 Eloquent auth 驱动程序。似乎无论如何您都在为您的用户模型使用 Eloquent - 所以现在应该有问题切换。
Change the following setup in your app/config/auth.php
file that Eloquent is being used - that should fix this issue for now:
更改app/config/auth.php
正在使用 Eloquent的文件中的以下设置- 现在应该可以解决此问题:
return array(
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/
'driver' => 'eloquent',
回答by vogomatix
Not sure if Laravel has changed since this was originally answered, but there is a simple answer:
不确定 Laravel 是否自最初回答以来发生了变化,但有一个简单的答案:
In your User.phpfile add:
在您的User.php文件中添加:
protected $rememberTokenName = 'myTokenField';
As far as I can tell this value is used by the getRememberTokenName
method and therefore propagates to the rest of the code.
据我所知,该值由该getRememberTokenName
方法使用,因此会传播到代码的其余部分。
As Laurenceindicated there may be a problem with the Database Auth driver which seems to use a hardcoded value.
正如 劳伦斯所指出的,似乎使用硬编码值的数据库身份验证驱动程序可能存在问题。
回答by omid kiani
Add a remember_key
column instead of remember_token
column to your users (or equivalent) database table.
向您的用户(或等效)数据库表中添加一remember_key
列而不是remember_token
列。
You should use along with that the following snippet:
您应该同时使用以下代码段:
public function getRememberToken()
{
return $this->remember_key;
}
public function setRememberToken($value)
{
$this->remember_key = $value;
}
public function getRememberTokenName()
{
return 'remember_key';
}