forked from shetabit/visitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee16a08
commit 3e117e3
Showing
11 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
database/migrations/2019_10_14_110318_create_visits_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateVisitsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('visits', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->mediumText('request')->nullable(); | ||
$table->text('languages')->nullable(); | ||
$table->text('useragent')->nullable(); | ||
$table->text('headers')->nullable(); | ||
$table->text('device')->nullable(); | ||
$table->text('platform')->nullable(); | ||
$table->text('browser')->nullable(); | ||
$table->text('ip')->nullable(); | ||
$table->nullableMorphs('visitable'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('visits'); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Shetabit\Visitor\Middlewares; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class LogVisits | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
$logHasSaved = false; | ||
|
||
// create log for first binded model | ||
foreach ($request->route()->parameters() as $parameter) { | ||
if ($parameter instanceof Model) { | ||
visitor()->visit($parameter); | ||
|
||
$logHasSaved = true; | ||
|
||
break; | ||
} | ||
} | ||
|
||
// create log for normal visits | ||
if (!$logHasSaved) { | ||
visitor()->visit(); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Shetabit\Visitor\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Visit extends Model | ||
{ | ||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'visits'; | ||
|
||
/** | ||
* The attributes that aren't mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'request', 'languages', 'useragent', | ||
'headers', 'device', 'platform', | ||
'browser', 'ip', | ||
]; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'request' => 'array', | ||
'languages' => 'array', | ||
'headers' => 'array', | ||
]; | ||
|
||
/** | ||
* Get the owning visitable model. | ||
*/ | ||
public function visitable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Shetabit\Visitor\Traits; | ||
|
||
use Shetabit\Visitor\Models\Visit; | ||
|
||
trait Visitable | ||
{ | ||
/** | ||
* Get all of the post's comments. | ||
* | ||
* @return mixed | ||
*/ | ||
public function visits() | ||
{ | ||
return $this->morphMany(Visit::class, 'visitable'); | ||
} | ||
|
||
/** | ||
* Create a visit log | ||
* | ||
* @return mixed | ||
*/ | ||
public function visit() | ||
{ | ||
$visitor = app('shetabit-visitor'); | ||
|
||
return $this->visits()->create([ | ||
'request' => $visitor->request(), | ||
'languages' => $visitor->languages(), | ||
'useragent' => $visitor->userAgent(), | ||
'headers' => $visitor->httpHeaders(), | ||
'device' => $visitor->device(), | ||
'platform' => $visitor->platform(), | ||
'browser' => $visitor->browser(), | ||
'ip' => $visitor->ip(), | ||
]); | ||
} | ||
|
||
/** | ||
* Alias for visits. | ||
* | ||
* @return mixed | ||
*/ | ||
public function views() | ||
{ | ||
return $this->visits(); | ||
} | ||
|
||
/** | ||
* Alias for visit | ||
* | ||
* @return mixed | ||
*/ | ||
public function view() | ||
{ | ||
return $this->visit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.