Skip to content

Commit

Permalink
fix package database table causing conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Sepehr committed Feb 22, 2021
1 parent f82da13 commit d391f7d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 110 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ We have the below methods to retrieve a visitor's information:
```php
$request->visitor()->browser(); // firefox
$request->visitor()->visit($post); // create log for post
$request->visitor()->setVisitor($user)->visit($post); // create a log wich says $user has visited $post
$request->visitor()->setVisitor($user)->visit($post); // create a log which says $user has visited $post
```

#### Store Logs
Expand Down
4 changes: 4 additions & 0 deletions config/visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
//except save request or route names
'except' => ['login','register'],


//name of the table which visit records should save in
'table_name' => 'shetabit_visits',

/*
|--------------------------------------------------------------------------
| List of Drivers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CreateVisitsTable extends Migration
*/
public function up()
{
Schema::create('visits', function (Blueprint $table) {
Schema::create(config('visitor.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('method')->nullable();
$table->mediumText('request')->nullable();
Expand All @@ -39,6 +39,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('visits');
Schema::dropIfExists(config('visitor.table_name'));
}
}
18 changes: 9 additions & 9 deletions src/Models/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

class Visit extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'visits';

public function __construct(array $attributes = [])
{
if (!isset($this->table)) {
$this->setTable(config('visitor.table_name'));
}
parent::__construct($attributes);
}
/**
* The attributes that aren't mass assignable.
*
Expand All @@ -31,9 +31,9 @@ class Visit extends Model
* @var array
*/
protected $casts = [
'request' => 'array',
'request' => 'array',
'languages' => 'array',
'headers' => 'array',
'headers' => 'array',
];

/**
Expand Down
17 changes: 8 additions & 9 deletions src/Provider/VisitorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ public function boot()
'config'
);

/**
* Migrations that needs to be done by user.
*/
$this->publishes(
[
__DIR__.'/../../database/migrations/' => database_path('migrations')
],
'migrations'
);
if (! class_exists('CreateVisitsTable')) {
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__ . '/../../database/migrations/create_visits_table.php.stub' => database_path("/migrations/{$timestamp}_create_visits_table.php"),
], 'migrations');
}


$this->registerMacroHelpers();
}
Expand Down
78 changes: 39 additions & 39 deletions src/Traits/CanVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +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->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;
}
/**
* 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(config('visitor.table_name') . ".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(config('visitor.table_name') . ".created_at", '>=', $time->toDateTime());
})->count() > 0;
}
}
100 changes: 50 additions & 50 deletions src/Traits/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +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);
$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;
}
/**
* 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(config('visitor.table_name') . ".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(config('visitor.table_name') . ".created_at", '>=', $time->toDateTime());
})->count() > 0;
}
}

0 comments on commit d391f7d

Please sign in to comment.