Auth :: user()在Laravel 5.2中返回null [英] Auth::user() returns null in Laravel 5.2

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

问题描述

我搜索并找到了各种类似的结果: auth()-> user()在Laravel 5.2中为空 Auth :: user()返回null

I have searched and found various results like these: auth()->user() is null in Laravel 5.2 and Auth::user() returns null

但是,我的仍然无法正常工作.

But, mine is still not working.

Auth::user()在控制器中起作用,但在模型中不起作用.返回null.

Auth::user() works in the controller, but not in the Model. It returns null.

代码是:

public function scopeOwned($query) {
    $query->where('user_id', '=', Auth::user()->id);
}

我尝试了dd(Auth::user()),它也返回了null.

I tried dd(Auth::user()) and it returns null as well.

有什么想法吗?

推荐答案

谢谢.

问题在这里解决了: https://laracasts.com/discuss/channels/laravel /authuser-returns-null-in-laravel-52

The problem solved here: https://laracasts.com/discuss/channels/laravel/authuser-returns-null-in-laravel-52

我必须直接将堆栈添加到中间件中(不在组中).

I had to add the stack into middleware directly (not in the group).

在/Http/kernel.php中:

in /Http/kernel.php:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class
];

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

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