从Collection中获取模型

Something very basic but I'm having a hard time solving this.

I have a list of users in the database that show as online users. I am fetching these users by their user_id

Model

public function scopeloggedInUser($query){
        return $query->select('user_id')->get();
}

when I var_dump or dd it shows that its a collection of a list of currently logged in users. (Said it was super simple).

enter image description here

I need to fetch those individual users. How do I dilute this to the individual user within the Online Model.

Within the Controller

public function index(Online $online)
        {
            $activeuser = $online->loggedInUser();
            return view('user.user', compact('activeuser'));
        }

In your online-model specify a relationship to the real user like this:

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

In your view you can now access each user in your foreach-loop like this:

foreach ($activeusers as $user)
{
    echo $user->user->username; // or whatever fields you need
}

But to be honest: in your case I wouldn't set up a new database table and new model if you need this functionality.

Move your logic to your User model and add a boolean field to your user table and change your query-scope to this (again: in your user model)

public function scopeOnline($query){
    return $query->where('online', 1);
}

You also shouldn't do a get() within a scope because then you have no more access to the query builder. For example: you want all logged in users that are female. With get: not pretty.

Without get:

User::online()->where('gender', '=', 'female')->get();