Skip to content

Commit

Permalink
Merge pull request shetabit#12 from mohammad76/master
Browse files Browse the repository at this point in the history
online users problem
  • Loading branch information
khanzadimahdi authored Mar 21, 2020
2 parents 95778e9 + 012be36 commit b63aa72
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/phpunit.xml export-ignore
/tests export-ignore
/ISSUE_TEMPLATE.md export-ignore
/PULL_REQUEST_TEMPLATE.md export-ignore
/PULL_REQUEST_TEMPLATE.md export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ You can count model visits like the below
```php
$model->visitLogs()->count();
```
unique users can be counted by their IP and by model.

```php
// by ip
$model->visitLogs()->distinct('ip')->count('ip');

// by user's model
$model->visitLogs()->visitor()->count();
```

use `Shetabit\Visitor\Traits\Visitor` in your `User` class, then you can run below codes

Expand Down
81 changes: 39 additions & 42 deletions src/Traits/CanVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,43 @@

trait CanVisit
{
/**
* Get all of the post's comments.
*
* @return mixed
*/
public function visitLogs()
{
return $this->morphMany(Visit::class, 'user');
}

/**
* Retrieve online users
*
* @param $query
* @param int $seconds
* @return mixed
*/
public function scopeOnline($query, $seconds = 180)
{
$time = now()->subSeconds($seconds);

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

/**
* check if user is online
*
* @param int $seconds
* @return bool
*/
public function isOnline($seconds = 180)
{
$time = now()->subSeconds($seconds);

return $this->visitLogs()->whereHasMorph('user', [static::class], function ($query) use ($time) {
$query
->where('user_id', $this->id)
->whereDate('visits.created_at', '>=', $time);
})->count() > 0;
}
/**
* Get all of the post's comments.
* @return mixed
*/
public function visitLogs()
{
return $this->morphMany(Visit::class, 'user');
}

/**
* Retrieve online users
* @param $query
* @param int $seconds
* @return mixed
*/
public function scopeOnline($query, $seconds = 180)
{
$time = now()->subSeconds($seconds);

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

/**
* check if user is online
* @param int $seconds
* @return bool
*/
public function isOnline($seconds = 180)
{
$time = now()->subSeconds($seconds);

return $this->visitLogs()->whereHasMorph('user', [ static::class ], function ($query) use ($time) {
$query
->where('user_id', $this->id)
->where('visits.created_at', '>=', $time->toDateTime());
})->count() > 0;
}
}
104 changes: 50 additions & 54 deletions src/Traits/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,54 @@

trait Visitor
{
/**
* Get all of the post's comments.
*
* @return mixed
*/
public function visits()
{
return $this->morphMany(Visit::class, 'visitor');
}

/**
* Create a visit log.
*
* @param Model|null $visitable
*
* @return mixed
*/
public function visit(?Model $visitable = null)
{
return app('shetabit-visitor')->setVisitor($this)->visit($visitable);
}

/**
* Retrieve online users
*
* @param $query
* @param int $seconds
* @return mixed
*/
public function scopeOnline($query, $seconds = 180)
{
$time = now()->subSeconds($seconds);

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

/**
* check if user is online
*
* @param int $seconds
* @return bool
*/
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;
}
/**
* Get all of the post's comments.
* @return mixed
*/
public function visits()
{
return $this->morphMany(Visit::class, 'visitor');
}

/**
* Create a visit log.
* @param Model|null $visitable
* @return mixed
*/
public function visit(?Model $visitable = NULL)
{
return app('shetabit-visitor')->setVisitor($this)->visit($visitable);
}

/**
* Retrieve online users
* @param $query
* @param int $seconds
* @return mixed
*/
public function scopeOnline($query, $seconds = 180)
{
$time = now()->subSeconds($seconds);

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

});
}

/**
* check if user is online
* @param int $seconds
* @return bool
*/
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;
}
}

0 comments on commit b63aa72

Please sign in to comment.