PHP code example of shetabit / visitor

1. Go to this page and download the library: Download shetabit/visitor 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/ */

    

shetabit / visitor example snippets


$request->visitor()->browser(); // firefox
$request->visitor()->visit($post); // create log for post
$request->visitor()->setVisitor($user)->visit($post); // create a log which says $user has visited $post

visitor()->visit(); // create a visit log

// or you can save log like the below
visitor()->visit($model);
// or like the below
$model->createVisitLog();

// you can say which user has visited the given $model
$model->createVisitLog($user);
// or like the below
visitor()->setVisitor($user)->visit($model);


$model->visitLogs()->count();

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

// by user's model
$model->visitLogs()->distinct('visitor_id')->count('visitor_id');

use Shetabit\Visitor\Models\Visit;

Visit::query()->where('route_name', 'posts.show')->count(); // visits of the posts.show route
visitor()->routeName(); // the name of the route of the current request
visitor()->route(); // the route of the current request

Visit::query()->where('route_name', 'posts.show')->update(['route_name' => 'articles.show']);

$visit->url;         // https://example.com/products/%D9%85%D8%AD%D8%B5%D9%88%D9%84
$visit->decoded_url; // https://example.com/products/محصول

$user->visit(); // create a visit log
$user->visit($model); // create a log which says, $user has visited $model
 

visitor()->onlineVisitors(User::class); // returns collection of online users
User::online()->get(); // another way

visitor()->isOnline($user); // determines if the given user is online
$user->isOnline(); // another way

'geoip' => true,

'geoip'    => true,
'resolver' => 'maxmind',
'maxmind'  => [
    'database' => storage_path('app/geoip/GeoLite2-City.mmdb'), // where you keep the downloaded database
    'locales'  => ['en'], // the languages of the country, region and city names, by preference
],

use GeoIp2\WebService\Client;
use Shetabit\Visitor\Resolvers\GeoIp\MaxMindResolver;

$this->app->singleton(MaxMindResolver::class, fn () => new MaxMindResolver(new Client($accountId, $licenseKey)));

visitor()->visit($post, ['name' => $request->input('name'), 'mobile' => $request->input('mobile')]);
$post->createVisitLog($user, ['mobile' => $request->input('mobile')]);
$user->visit($post, ['mobile' => $request->input('mobile')]);

$visit->extra_attributes['mobile'];

Visit::query()->where('extra_attributes->mobile', '09120000000')->get();

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Shetabit\Visitor\Models\Visit as BaseVisit;

class Visit extends BaseVisit
{
    protected function mobile(): Attribute
    {
        return Attribute::get(fn () => $this->extra_attributes['mobile'] ?? null);
    }
}

'model' => \App\Models\Visit::class,

'connection' => 'tenant',
bash
php artisan vendor:publish --tag=visitor-migrations

php artisan migrate
bash
php artisan vendor:publish --tag=visitor-config