Laravel身份验证-Auth :: check返回FALSE [英] Laravel Authentication - Auth::check returns FALSE

查看:79
本文介绍了Laravel身份验证-Auth :: check返回FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Laravel 还是很陌生,我目前正在为我的项目进行手动身份验证.
我已经尝试过 Auth :: attempt 来验证已输入的凭据,而且我看到它可以正常工作,因为我已被重定向到认证时想要的页面.

I'm fairly new to Laravel and I'm currently working on a manual authentication for my project.
I have tried Auth::attempt to validate the credentials that have been input, and I have seen that it works since I am being redirected to the page I wanted when it is authenticated.

但是,当我尝试在已重定向到的视图中执行 Auth :: check 时,似乎它返回FALSE并且不读取经过身份验证的用户.这使得该视图的某些部分不可见.关于这个有什么见解吗?非常感谢!

However, when I try to do Auth::check in the view I have been redirected to, it seems that it returns FALSE and does not read the authenticated user. This makes the certain portion of that view to not be visible. Any insights on this one? Thanks a lot!

控制器

namespace App\Http\Controllers;
use DB;
use Session;
use Illuminate\Http\Request;
use Auth;    
class LoginController extends Controller
{   //    
    public function index()
    {
        return view('view_login');
    }    

    public function verify(Request $request)
    {
        $credentials = $request->only('username', 'password');    
         if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect('dashboard');
        }
        else
        {
            return redirect('/');
        }
    }

查看

@if (Auth::check())
     <li><a href="dashboard.html"> Hello {{ Auth::user()->username }}  </a></li>
@endif 

当前结果:

预期结果:

更新:我试图将 dd(auth()-> user()); 放在身份验证之后,并且已经看到了身份验证用户的属性.但是,将其放置在仪表板控制器上之后,它将返回NULL.有人可以帮我吗?

Update: I've tried to put dd(auth()->user()); after authentication, and I have seen the attributes of the authenticated user. However, after putting this on the Dashboard Controller, it returns NULL. Can someone please help me on this?

推荐答案

检查用户数据库的主键.如果您设置表的主键而不是 id ,则需要在用户模型中进行设置.

Check the primary key of your users database. If you set your table's primary key other than id, you need to set it in your User model.

Laravel文档规定以下内容:

The Laravel Documentation states the following:

Eloquent也将假定每个表都有一个名为id的主键列.您可以定义一个受保护的$ primaryKey属性以覆盖此约定.

例如,如果在用户数据库中将user_id列设置为主键,则需要将以下代码放置在用户模型中:

For example, if you have a user_id column set as your primary key in your users database, you need to place the code below in the User model:

受保护的$ primaryKey ='user_id';

这篇关于Laravel身份验证-Auth :: check返回FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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