PHP code example of ami-hp / laravel-eye

1. Go to this page and download the library: Download ami-hp/laravel-eye library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ami-hp / laravel-eye example snippets


   'table_name' => 'eye_visits',
   

   'cache' => [  
       'key' => 'eye__records',  
       'max_count' => 1000000,  
   ],
   

   'cookie' =>[  
       'key' => 'eye__visitor',  
       'expire_time' => 2628000, //in minutes aka 5 years  
   ],
   

   'default_parser' => 'jenssegers',
   

   'ignore_bots' => true,
   

   'queue' => true,
   

   Schema::create(config('eye.table_name'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->mediumText('unique_id')->nullable();
            $table->string('method')->nullable();
            $table->mediumText('request')->nullable();
            $table->mediumText('url')->nullable();
            $table->mediumText('referer')->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->ipAddress('ip')->nullable();
            $table->string('collection')->nullable();
            $table->nullableMorphs('visitable'); // object model
            $table->nullableMorphs('visitor'); // subject model
            $table->timestamp('viewed_at')->useCurrent();
        });

$post = Post::first();
$user = User::first();
$name = 'name of collection';

//if you wanted to set data
$eye = eye($post)->setVisitor($user)->setCollection($name);

//also if you didn't want to set data you can
$eye = eye();

$post = Post::first();

eye($post)
//or
eye()->setVisitable($post);

$user = User::first();

eye()->setVisitor($user);

$name = 'name of collection';

eye()->setCollection($name);

eye()->viaDatabase();
//or
eye()->viaCache()

eye($post)->viaDatabase();

eye($post)->viaCache();

$eye = eye()->viaDatabase(); //OR
$eye = eye()->viaCache();

$post = Post::first();
$user = User::first();
$name = 'name of collection';

$eye->collection($name)
    ->visitor($user)
    ->visitable($user);

$post = Post::first();

eye()->viaCache()->visitable($post);
//SAME AS
eye($post)->viaCache();

$user = User::first();

eye()->viaCache()->visitor($user);
//SAME AS
eye()->setVisitor($user)->viaCache();

$name = 'name of collection';

eye()->viaCache()->collection($user);
//SAME AS
eye()->setCollection($name)->viaCache();

$name = 'name of collection';

eye()->viaCache()->once();

Step1->Step2->Step3->Step4->record(bool $once = false, ?Model $visitable = null, ?Model $visitor = null);

$post = Post::first();
$user = User::first();

eye()->viaCache()->record(true , $post , $user);
//SAME AS
eye($post)->viaCache()->visitor($user)->once()->record();

$post = Post::first();
$user = User::first();
$name = 'name of collection';

eye()->viaDatabase()->collection($name)->visitable($post)->record();
eye($post)->viaCache()->once()->record(); // Recommended

eye()->viaCache() 
// OR
eye()->viaDatabase()

$post = Post::first(); // or NULL

eye($post)->viaCache(); // Simplified
//or
eye()->setVisitable($post)->viaCache();
//or
eye()->viaCache()->visitable($post); // Humanized

->where('visitable_id'   , $post->id)
->where('visitable_type' , get_class($post))

eye()->viaCache();

->where('url' , request()->fullUrl())

eye()->viaCache()->visitable(false);

$user = User::first(); // or NULL

eye()->viaCache()->visitor($user , true);

->where('visitor_id'   , $user->id)
->where('visitor_type' , get_class($user))

$name = 'name of collection';

eye()->viaCache()->collection($name);

->where('collection' , $name)

$startDateTime = '1997-01';
$endDateTime   = '2023-08-17 10:11:00';
$period = Period::create($startDateTime , $endDateTime); //example

eye()->viaCache()->period($period);

if ($startDateTime !== null && $endDateTime === null) 
    ->where('viewed_at', '>=', $startDateTime);
elseif ($startDateTime === null && $endDateTime !== null)
    ->where('viewed_at', '<=', $endDateTime);
elseif ($startDateTime !== null && $endDateTime !== null)
    ->whereBetween('viewed_at', [$startDateTime, $endDateTime]);

$column = 'unique_id';
eye()->viaCache()->unique($column);


->unique($column)

$column = 'unique_id';
eye()->viaCache()->unique($column)->count();

->distinct()->count($column);

Step1->Step2->count(); // returns Int
Step1->Step2->get();   // returns collection of Visit models

eye($post)->viaCache()
          ->unique()
          ->count();
// OR
eye()->viaDatabase()
     ->collection('name')
     ->visitor($user)
     ->get();

eye()
// or
eye()->via('database' , 'cache'); // means both of them


$post = Post::first();

eye($post);
//or
eye()->visitable($post);

$user = User::first();

eye($post);
//or
eye()->visitor($post);

$name = 'collection';

eye($post)->collection($name);

$startDateTime = '1997-01';
$endDateTime   = '2023-08-17 10:11:00';
$period = Period::create($startDateTime , $endDateTime); //example

eye()->period($period);

$column = 'platform';

eye()->unique($column);

eye($post)->collection($name)->unique()->count();
eye()->via('cache' , 'database')->visitor($user)->get();

eye()->truncate(); // Removes All Visits in Every Storage
eye()->viaCache()->truncate(); // Removes All Visits in a storage
eye()->via('database' , 'cache')->truncate(); // Removes All Visits in selected storages


eye($post)->collection($name)->delete();
eye()->viaCache()->visitor($user , true)->delete();

eye()->via('cache')
     ->visitable(false)
     ->visitor($user , true)
     ->collection($name)
     ->delete();

public function deleted(Model $visitable)
{
    if ($visitable->isForceDeleting())
        eye($visitable)->delete();
}

public function visits()
{
    return $this->morphMany(Visit::class, 'visitable');
}

$posts = Post::with('visits')->get();

foreach ($posts as $post){
    $post->visits; // collection of visits
}

//OR

$posts = Post::withCount('visits')->get();

foreach ($posts as $post){
    $post->visits_count; // int
}

public function cachedVisits(?string $unique = null , ?string $collection = null , ?Model $visitor = null): Collection  

$visits = $post->cachedVisits(); //collection of visits
// OR
$visits = $post->cachedVisits('unique_id' , 'name of collection' , $user);


$visits->count(); //int

public function deleted(Model $visitor)
{
    if ($visitor->isForceDeleting())
        eye()->visitor($visitor)->visitable(false)->delete();
}

public function visits()
{
    return $this->morphMany(Visit::class, 'visitor');
}

public function cachedVisits(?string $unique = null , ?string $collection = null , $visitable = false): Collection
winbatch
$ composer or:publish --provider="Ami\Eye\Providers\EyeServiceProvider"
$ php artisan migrate