I'm trying to list online users for Laravel 5. But the user sessions table in my column was constantly staying null. Don't updating the user ID to the session table.
How can I solve this problem?
My Online Model:
<?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' ); } }Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community