First, create a table into which the input user ID will be inserted.
Schema::create('active_users', function(Blueprint $table) { $table->increments('id')->unsigned(); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users') ->onUpdate('cascade')->onDelete('cascade'); $table->timestamps(); });
Then insert data into your controller during login
if (Auth::attempt($credentials)) { DB::table('active_users')->insert(array('user_id' => Auth::id())); }
and delete data during exit
DB::table('active_users')->where('user_id', '=', Auth::id())->delete();
Print a list of online users in your view
<ul><strong>Online Users</strong> <?php $online_users = DB::table('active_users')->where('user_id','!=',Auth::id())->get(); ?> @foreach($online_users as $online_user) <li>{{User::find($online_user->user_id)->first_name}}</li> @endforeach </ul>
source share