Laravel 5.8:记住我令牌没有保存在数据库中 [英] Laravel 5.8 : Remember me token not being saved in database

查看:68
本文介绍了Laravel 5.8:记住我令牌没有保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令牌创建为cookie,我可以在浏览器中看到它,但未将其保存在数据库中.

The token is created as a cookie, I can see it in my browser, but it's not being saved in the database.

这是我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    public $timestamps = false;

    protected $table = 'accounts';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'Username', 'email', 'Password', 'Nickname', 'SecretQuestion', 'SecretAnswer', 
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'Password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

我在LoginController中的登录方法:

My login method in my LoginController :

public function login(Request $request)
    {
        $user = User::where('Username', $request->Username)
                    ->where('Password', md5($request->password))
                    ->first();
        if (is_null($user)) return back()->with('error', 'Votre identifiant ou mot de passe est incorrect.');
        Auth::login($user, $request->remember == 'on' ? true : false);
        if (Auth::check()) return Redirect::to('/');
    }

当我检查或不记住记住选项时,它会相应地使用带有true或false参数的Auth :: login()方法.但是,虽然我可以在浏览器 remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d 中看到令牌,但是在我的数据库中,从未设置该字段.

When I check or not the remember_me option, it uses the Auth::login() method accordingly, with true or false argument. However, in my database, the field is never set, although I can see a token in my browser remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d.

我的数据库中的字段: remember_token varchar(100)null,

The field in my db : remember_token varchar(100) null,

推荐答案

好,我在寻找另一个问题时找到了答案.

Ok, I found the answer while searching for another problem.

这是由于我的帐户表中的id字段未小写命名为"id".我的字段是"Id".(不是我的选择,我知道这很糟糕).

It was due to my id field in my accounts table not being named 'id' in lowercase. My field was 'Id'. (Not my choice, I know it's bad).

要修复此问题,我必须在用户模型中添加: protected $ primaryKey ='Id'; .

To fix it, I had to add : protected $primaryKey = 'Id'; to my User model.

这篇关于Laravel 5.8:记住我令牌没有保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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