Auth类和auth()函数在Eloquent模型中不起作用. (Laravel 5) [英] Auth class and auth() function doesnt works in Eloquent model. (Laravel 5)

查看:149
本文介绍了Auth类和auth()函数在Eloquent模型中不起作用. (Laravel 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从会话表中获取在线用户. 为此,我首先在会话表中添加了user_id列,然后在刷新页面时尝试更新此user_id.这是Online.php模型,其中scopeUpdateCurrent()函数检查用户并更新会话表中的user_id:

Im trying get online users from sessions table. To do this, first I added user_id column to sessions table, then I trying update this user_id whenever the page is refreshed. Here is the Online.php model where scopeUpdateCurrent() function check the user and update user_id in sessions table:

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Session;
use Auth;

class Online extends Model
{
  public $table = 'sessions';

  public $timestamps = false;

  public function user()
  {
    return $this->belongsTo('App\User');
  }

  ...

  public function scopeUpdateCurrent(Builder $query)
  {
    $user = auth()->user(); // returns null but user logged in
    // $user = Auth::user(); // returns null too

    return $query->where('id', Session::getId())->update([
        'user_id' => $user ? $user->id : null
    ]);
  }

}

这是AppServiceProvider.php,我将其称为更新:

Here is the AppServiceProvider.php where I call that updating:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Online;

class AppServiceProvider extends ServiceProvider
{
  public function boot()
  {
    Online::updateCurrent();
    ...
  }
  ...
}

问题是auth()-> user()或Auth :: user()在Online.php模型中返回null,但用户已登录.例如,在某些View或routes.php中,它返回有效的用户对象.

The problem is that auth()->user() or Auth::user() returns null in Online.php model but user logged in. For example in some View or in routes.php it returns valid user object.

可能不太清楚,因为我不太会英语.如果在评论中写道,我将尝试详细描述问题

May not be very clear, because I badly know English. If that write in comments and I will try to describe the issue in detail

推荐答案

这不是雄辩模型的问题.您正在尝试在会话开始之前获取经过身份验证的用户.

This is not a problem with Eloquent models. You are trying to get the authenticated user before sessions have even started.

例如,在app/Http/Kernel.php中,您将看到一个中间件列表.其中之一称为Illuminate\Session\Middleware\StartSession,它负责启动会话.

For example, in app/Http/Kernel.php, you'll see a list of middlewares. One of them is called Illuminate\Session\Middleware\StartSession, which is responsible for starting your session.

您的服务提供商尚未访问这些会话数据,因为它尚未启动.中间件将在生命周期的后期加入.

Your service provider doesn't have access to these session data because it hasn't started yet. Middlewares kick in later in the lifecycle.

解决方案是创建一个中间件来处理此问题.在您的自定义中间件中,您可以添加一些代码,它就会起作用.

The solution would be to create a middleware to handle this. In your custom middleware, you can add your bit of code, and it will work.

但是,如果您使用的是L5.2,则默认的数据库会话处理程序已经包含user_id并为您更新它.

If you're using L5.2 though, the default database session handler already includes user_id and updates it for you.

这篇关于Auth类和auth()函数在Eloquent模型中不起作用. (Laravel 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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