0

I have create a middleware where i check for user status(online, ofline).

 public function handle(Request $request, Closure $next)
    {
        if (Auth::check()) {
            $expiresAt = now()->addMinutes(1); // keep online for 1 min 
            Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
  
            // Update on db last_online_at with now time
            User::where('id', Auth::user()->id)->update(['last_online_at' => now()]);
        }

        return $next($request);
    }

So update also and last_online_at clumn on db. But what i am trying to do is to see on frontend a status online or ofline. And i have created a model method

    // Check if user is online
    public function isOnline()
    {
        return Cache::has('user-is-online-' . $this->id);
    }

But i dont know how to pass isOnline value on vue table

 <td>
     <span class="badge badge-warning">{{user.last_online_at}}</span>
 </td>
 <td>
   <span class="badge badge-success">Online</span>
   <span class="badge badge-danger">Offline</span>
 </td>

methods: {
  axios.post('/admin/users/getusers?page=' + this.pagination.current_page, {
                    perPage: this.displayRecord
                })
                .then(response => {
                    this.users = response.data.data
}


        computed: {

            getOnlineStatus() {
                var onlineUser = '';
                var now = new Date();
                this.users.forEach(function(user) {
                    onlineUser.push(user.last_online_at)
                })
                console.log(onlineUser);

                if(now == onlineUser) {
                    // want to print true or false i think
                }
            }

        },

laravel controller

 public function getUsers(Request $request)
    {
        abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $paginate = $request->perPage;

        return new UserResource(User::paginate($paginate));
    }
Maison012
  • 99
  • 1
  • 9
  • Add a v-if like this to your Online/Offline spans `v-if="users.last_online_at == some datetime"` or create a computed property that checks if user.last_online_at is within a certain time range and returns a boolean. Then use the computed property in your v-if – bassxzero Nov 11 '22 at 10:35
  • i have tryed to create a computed property but it does not work. I print the result on console and every time i saw false – Maison012 Nov 11 '22 at 11:04
  • Post your computed property code. – bassxzero Nov 11 '22 at 11:06
  • just updated question. THis is what i get on console `(2) ['2022-11-11 11:18:37', '2022-11-11 09:39:13'] 0 : "2022-11-11 11:18:37" 1 : "2022-11-11 09:39:13"` – Maison012 Nov 11 '22 at 11:17
  • `user.last_online_at` is going to be a string when it comes back from Laravel. So you need to convert it to a date so you can do comparisons with it. So `onlineUser.push(user.last_online_at)` should be something like `onlineUser.push(new Date(user.last_online_at))` . Also, you declared `onlineUser` as a string. So it won't have a `.push` method. Finally, in your comparison `if(now == onlineUser)` , dates have second/millisecond level precision so you need to to compare each part of the date instead of using `==` since the dates will never be equal. – bassxzero Nov 11 '22 at 11:23
  • You probably want to do something like this to see if the user was on in the last hour, min, 5 mins, whatever time amount you decide is enough to count them as "online". https://stackoverflow.com/questions/9224773/check-if-date-is-less-than-1-hour-ago – bassxzero Nov 11 '22 at 11:26
  • But there i compare all users. How can i compare each user. – Maison012 Nov 11 '22 at 11:29
  • Make `getOnlineStatus()`a method instead of computed. Change it to accept a date in the method like `getOnlineStatus(testDate)`. Then in your template call the method and pass in the user's last_online_at stamp like ` – bassxzero Nov 11 '22 at 11:34

1 Answers1

1

I haven't tested this, but is should be something like this.


<td>
     <span class="badge badge-warning">{{user.last_online_at}}</span>
 </td>
 <td>
   <span v-if="getOnlineStatus(user.last_online_at)" class="badge badge-success">Online</span>
   <span v-else class="badge badge-danger">Offline</span>
 </td>

methods: {
  ...someMethods,
 getOnlineStatus(testDate) {
                var myDate = new Date(testDate);
                var now = new Date();
                let FIVE_MINS = 60 * 5;
                return ((now - myDate) < FIVE_MINS);

            
            }
}
      


bassxzero
  • 4,838
  • 22
  • 34
  • Yes this shows me the online or offline status but with an problem. When i refresh for first time i see my admin user with witch iam loged in `online` if refresh more than one time page i see offline status. `Some time when i reload page i see online status some time i see offline` – Maison012 Nov 11 '22 at 11:49
  • @leoon0012 Add `console.log(testDate)` and `console.log(myDate)` in your `getOnlineStatus()` and see if your method is getting the correct data or if you are rendering too soon or something. – bassxzero Nov 11 '22 at 11:53
  • Every time i see mached date `testDate 2022-11-11 12:56:22 app.js:22682 myDate Fri Nov 11 2022 12:56:22 GMT+0100 (Central European Standard Time)` but maybe the problem is formatting? – Maison012 Nov 11 '22 at 11:57
  • What does `console.log(now)` and `console.log((now - myDate))` output? – bassxzero Nov 11 '22 at 11:59
  • This is the response `now Fri Nov 11 2022 13:01:22 GMT+0100 (Central European Standard Time) app.js:22683 now+mydate 711 app.js:22682 now: Fri Nov 11 2022 13:01:22 GMT+0100 (Central European Standard Time) app.js:22683 now+mydate: 12129712` – Maison012 Nov 11 '22 at 12:01
  • `now+mydate` ? Do you mean `now - mydate` ? – bassxzero Nov 11 '22 at 12:04
  • @leoon0012 Did you fix it? – bassxzero Nov 11 '22 at 12:36
  • `now+mydate ? Do you mean now - mydate ? ` is just a string i have added on `console log` to make more clear the result. No i have not fixed yet – Maison012 Nov 11 '22 at 13:02
  • This is the result from console now and console now-myDate `Fri Nov 11 2022 14:02:53 GMT+0100 (Central European Standard Time) app.js:22683 368 app.js:22682 Fri Nov 11 2022 14:02:53 GMT+0100 (Central European Standard Time) app.js:22683 15820368` . I does not understand why on view show offline status there are mached value? – Maison012 Nov 11 '22 at 13:04
  • Any other idea about this problem? I have 2 users right now on table – Maison012 Nov 11 '22 at 13:21
  • I'm pretty sure your dates are in the wrong format for the Date constructor to parse. Try returning them in iso-8601 format https://stackoverflow.com/questions/4829569/parsing-iso-8601-date-in-javascript – bassxzero Nov 11 '22 at 13:22
  • I think the problem was here: `let FIVE_MINS = 5 * 60 * 1000` i tryed this and now user show offline status after 5 minutes. But i think 5 minute take too long. How about 1 minutes ? – Maison012 Nov 11 '22 at 13:32
  • 1 minute is more reasonable. I just randomly picked 5 mins. – bassxzero Nov 11 '22 at 13:34
  • Okay now it work, but do you thing there is another method to make this function reactive. I mean to change status without reload? – Maison012 Nov 11 '22 at 13:38
  • @leoon0012 yeah there is a way to update the status without reloading. You could just rerun this axios fetch when you want to update the data `/admin/users/getusers?page=` or you could create a new back end endpoint that only returns the `last_online_at` for users, call that endpoint, then update your user data. – bassxzero Nov 11 '22 at 13:45
  • I was thinking to make something on vue mounted and to make to reload this method every 1 minute or 2 minute, just don know how to pass value from mounted to users table. Couse if i use this method on `getData` method i thing still need reload to make changers for user who are offline – Maison012 Nov 11 '22 at 13:57
  • @leoon0012 Post what you have a new question and we can work on it. – bassxzero Nov 11 '22 at 14:26