I used something very similar and it's good. You attach a user_id to the session table so that it links the session data to a relation (users table). You can then show users online and guests within a specific time frame (last 10 minutes).
Show Online Users and Guests
I'm developing a syste using Laravel 5 and I think it will be good to show online users and guests. I have lots stumbled upon this old link http://laravel.io/forum/03-03-2014-sentry-3-users-online and I don't know whether this is the best way to do it.
Anyone with a good and updated version?
let me see your code
Example for Model - Session.php
<?php
use Carbon\Carbon;
class Session extends \Eloquent {
protected $hidden = ['payload'];
/**
* The database table used by the model.
*
* @var string
*/
public $table = 'sessions';
public $timestamps = false;
/**
* Returns the user that belongs to this entry.
*/
public function user()
{
return $this->belongsTo('User');
}
/**
* Returns all the guest users.
*
* @param $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGuests($query)
{
return $query->whereNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(Config::get('custom.activity_limit'))));
}
/**
* Returns all the registered users.
*
* @param $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRegistered($query)
{
return $query->whereNotNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(Config::get('custom.activity_limit'))))->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([
'user_id' => ! empty(Auth::user()) ? Auth::id() : null
]);
}
}
Hey @bashy I'm having a problem with this script.
Every time I reload the page, Session::getId() returns a different value so there is no way Session::updateCurrent() will ever update the same row. (i.e change user_id from null to Auth::id() )
I actually use the model of "Online" but works fine for me
Hi @bashy
how i can add your script .
do i have to put in route controller this " scopeUpdateCurrent(auth::user());"
Thank you .
@khaledsmq This was for v4.2 but you can somehow do it before the app loads.
Laravel 4.2
App::before(function($request)
{
// Update current online etc
Online::updateCurrent();
}
It doesn't ever get deleted, does it? I mean it doesn't expires and doesn't get cleared on logout either.
@dishark It's based on Laravel sessions in the database so it's up to that...?
Sorry for necro posting, but has anyone gotten this to work with Laravel 5.1? I keep trying but no matter what it doesn't work. There seems to be a new session ID every page load (Which I think it's supposed to do now for security?) So, I don't know how it stores the session in the DB if it keeps changing. I'd really like to get a list of currently online users. So if this isn't the way I'd love to know of a better one.
Edit: Got the session ID to chill out now that I setup the config correctly. Derp. However, now that it is finding the ID, it's not updating it. doing a dd() in the updateCurrent() returns null for auth even though I'm signed in.
Edit2: Got it working. Seems hacky though. Made a middleware and added it to a route group and surrounded all my routes. It works now though.
@bashy I think we should use Socket-io for this instead of using Laravel sessions .
@tangoG Do what you think is best for your app.
i have same problem "Every time I reload the page, Session::getId() returns a different value". how to fix what?
I recently just did this in a super simple way with the laravel cache. It's not super accurate but it gets the job done.
https://erikbelusic.com/tracking-if-a-user-is-online-in-laravel/
Both are winner, I have to write function to an old app 4.1 and write app for 5.2, this cover it all
HI i am using Laravel 4.2 version . and i have used this method but i am not getting any error it not working in my site i have follow all the steps . after login it not insert login user data in session table . can you please let me know how it work . i need my alll online user show in row
@shintu Open a new thread. Bear in mind that 4.2 is old now and may be slow for responses.
I found here is a nice solution... http://larademy.com/daily/tracking-user-currently-online-laravel-easy-way/
@tisuchi You just stole that article and shamelessly spam it here?
Here is the original article where it was stolen from: https://erikbelusic.com/tracking-if-a-user-is-online-in-laravel/
Sorry Ecky...
I didn't stole your article.
However, I checked on that article that referred your original article. That site is a completely referral site of laravel.
By the way... I am not a spammer.
@tisuchi You are a liar. That site is a spam site. You start the article with "X wrote a nice..." and then copy paste the entire thing and surround it with ads.
@tisuchi And to prove that you are a liar, a thief and a spammer. Twitter name: https://twitter.com/tisuchi ( Thouhedul Islam )
Registration details for larademy.com:
Domain Name: larademy.com
Registrar URL: http://www.godaddy.com
Registrant Name: Thouhedul Islamm
Registrant Organization:
Name Server: NS1.BLUEHOST.COM Name Server: NS2.BLUEHOST.COM DNSSEC: unsigned
This forum is not intended to post this kind of stuff, so please delete these messages after looking into it. Please don't let this guy spam his stolen stuff here.
Nope... I think you are blaming me wrongly...
There are many sites those are referring laravel articles all over the world. If I refer their site / article as a tutorial note, I believe nothing wrong...
@tisuchi Please stop lying. You know damn well it's wrong, because you are making adsense money off it. Just mentioning the author's name does not give you the right to copy-paste the whole thing and make money of it.
Does learninglaravel.net doing such kind of things? There are many site those are referring others article with a proper manner. So, whats wrong with larademy.com?
I maid reference in a proper way by announcing your name and article reference in order to read full.
@tisuchi No, you are lying again. You copy paste the whole thing, not just an excerpt with a link to 'read more'. You are not referring, you are copy pasting everything and monetizing it.
I think its wasting my time to explain here...
@tisuchi Stop stealing and lying please.
I understand Ecky is being strong, but maybe you could read about what is called fair use to understand his objections:
http://www.nolo.com/legal-encyclopedia/fair-use-rule-copyright-material-30100.html
If your site is a news organization people are going to expect the highest standards from you.
I took a look at the site you mentioned and it didn't seem to have tutorials reposted without the original author's permission.
@erikbelusic I've checked your tutorial posted here https://erikbelusic.com/tracking-if-a-user-is-online-in-laravel/
i think you have the best / simplest aproach to list online users, unfortunitly i have deferent implementation on my app, My recent online users list are avaliable on all dashboard pages with a button to show full list of on line users.
So how would you approach this issue.
1- Get recent 10 Logged in users 2- Make the list avaliable thought all pages 3- Redirect user to full list of online users when they click on the (view all button)
Please or to participate in this conversation.