Laravel 5更新会话表user_id列

时间:2015-12-04 09:20:56

标签: php mysql laravel-5

我正在尝试列出Laravel 5的在线用户。但是我的专栏中的用户会话表始终保持为空。不要将用户ID更新为会话表。

我该如何解决这个问题?

我的在线模特:

<?php namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;
use Session;

class Online extends Eloquent{

    protected $hidden = ['payload'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    public $table = 'sessions';

    public $timestamps = false;
    protected $fillable = ['user_id'];
    /**
     * Returns all the guest users.
     *
     * @param  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeGuests($query)
    {
        return $query->whereNull('user_id');
    }

    /**
     * Returns all the registered users.
     *
     * @param  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeRegistered($query)
    {
        return $query->whereNotNull('user_id')->with('user');
    }

    /**
     * Updates the session of the current user.
     *
     * @param  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeUpdateCurrent($query)
    {
        return $query->where('id', Session::getId())->update(array(
                'user_id' => !trim(\Auth::check())==false ? \Auth::user()->id : null
            ));
    }

    /**
     * Returns the user that belongs to this entry.
     */
    public function user()
    {
        return $this->belongsTo('User');
    }
}

我的AppServiceProvider:

<?php namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(){
        Online::updateCurrent();
        view()->share('onlineusers', Online::registered()->get());

    }

    /**
     * Register any application services.
     *
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * "Registrar" implementation here. You can add your own bindings too!
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'Illuminate\Contracts\Auth\Registrar',
            'App\Services\Registrar'
        );
    }

}

0 个答案:

没有答案