shetabit/visitor

problem in online users

mohammad76 opened this issue · 8 comments

I have found a problem in scopeOnline function you should change this

public function scopeOnline($query, $seconds = 180)
    {
        $time = now()->subSeconds($seconds);

        return $query->whereHas('visits', function ($query) use ($time) {
            $query->whereDate('visits.created_at', '>=', $time);
        });
    }

to this

    public function scopeOnline($query, $seconds = 180)
    {
        $time = now()->subSeconds($seconds);

        $query->whereHas('visits', function ($query) use ($time) {
            $query->where('visits.created_at', '>=', $time->toDateTime());

        });
    }

because when you check visits.created_at with whereDate it only checks the date and the time will be ignored

oh I found this problem in isOnline function too.

you should change this:

public function isOnline($seconds = 180)
    {
        $time = now()->subSeconds($seconds);

        return $this->visits()->whereHasMorph('visitor', [static::class], function ($query) use ($time) {
            $query
                ->where('visitor_id', $this->id)
                ->whereDate('visits.created_at', '>=', $time);
        })->count() > 0;
    }

to this:

  public function isOnline($seconds = 180)
    {
        $time = now()->subSeconds($seconds);

        return $this->visits()->whereHasMorph('visitor', [static::class], function ($query) use ($time) {
                $query
                    ->where('visitor_id', $this->id)
                    ->where('visits.created_at', '>=', $time->toDateTime());
            })->count() > 0;
    }

what's wrong with it? it will be auto convert to string timestamp.
have you test it?

yes I test that, whereDate only checked the date, it didn't check the time.

you can check in laravel docs:
https://laravel.com/docs/6.x/queries

it has wrong results? have you try it?

yes and yes, it's got all online users in today - not just 180 seconds ago

could you fix it and create a pull request ? i will merge and release a new version.
thanks.

No problem, I will fixed it

Thanks.

has merged. if you have any new idea or feature, will be accepted. just create pull requests :)

thanks.