1. Go to this page and download the library: Download niladam/laravel-visits 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/ */
niladam / laravel-visits example snippets
namespace App\Models;
use Niladam\LaravelVisits\Concerns\CanHaveVisits; // <--- add this to your model
use Niladam\LaravelVisits\Concerns\HasVisitsTrait; // <--- add this to your model
class Product extends Model implements CanHaveVisits // <--- add this to your model
{
use HasVisitsTrait; // <-- add this to your model
}
use Niladam\LaravelVisits\Middleware\RecordVisitsMiddleware;
Route::get('/products/{product}', function (Product $product) {
return view('products.show', [
'product' => $product,
]);
})->middleware(RecordVisitsMiddleware::class);
$product = Product::latest()->first();
lvVisit(
visitable: $product,
count: 1, // optional, defaults to 1
referer: 'some-referer', // optional, defaults to null
platform: 'desktop', // optional, defaults to null, and will be determined from the request
ipAddress: '127.0.0.1', // optional, defaults to null, and will be set as the request IP or '127.0.0.1'
async: true // optional, defaults to true. If set to false, the visit will be recorded synchronously
); // <-- This will dispatch a job to create the visit
$url = 'https://my-cool-domain.app/some-url';
lvVisit(
visitable: $url,
count: 4,
async: true
); // <-- This will dispatch a job to create 4 visits.
$product = Product::latest()->first();
$product->visits; // <-- returns a collection of visits
$product = Product::latest()->first();
$product->visits()->desktop()->get(); // <-- returns a collection of visits from desktop users
$product->visits()->phone()->get(); // <-- returns a collection of visits from phone users
$product->visits()->tablet()->get(); // <-- returns a collection of visits from tablet users
$product->visits()->robot()->get(); // <-- returns a collection of visits from robots
$product->visits()->other()->get(); // <-- returns a collection of visits from other platforms
$product->visits()->mobile()->get(); // <-- returns a collection of visits mobile (phone + tablet) users
$product->visits()->entities()->get(); // <-- returns a collection of visits to entities
$product->visits()->urls()->get(); // <-- returns a collection of visits to URLs
$product = Product::latest()->first();
$product->visits->first()->ip; // <-- returns the IP address as a string
return [
/**
* When saving the URLs to the database, it might make sense to remove
* the current application URL from the logged URL.
*/
'remove_current_app_domain' => env('LARAVEL_VISITS_REMOVE_CURRENT_APP_DOMAIN', true),
/**
* When saving the URLs to the database, it might make sense to remove some strings
* from it. You can define them here. These will be removed from the URL
* that will be saved.
*/
'replace_in_urls' => [],
/**
* Overwrite these values to use your own models, jobs and middleware.
*/
'overwrites' => [
'user_model' => '\App\Models\User',
'model' => \Niladam\LaravelVisits\Models\Visit::class,
'job' => \Niladam\LaravelVisits\Jobs\RecordVisitJob::class,
'job_url' => \Niladam\LaravelVisits\Jobs\RecordVisitUrlJob::class,
'middleware' => \Niladam\LaravelVisits\Middleware\RecordVisitsMiddleware::class,
],
];