PHP code example of binafy / laravel-user-monitoring

1. Go to this page and download the library: Download binafy/laravel-user-monitoring 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/ */

    

binafy / laravel-user-monitoring example snippets


/*
 * Main configuration settings for the package.
 */
'config' => [
    'routes' => [
        /*
         * Path to the route file that handles user monitoring routes.
         */
        'file_path' => 'routes/user-monitoring.php',
    ],
],

'user' => [
    /*
     * Specify the fully qualified class name of the user model.
     */
    'model' => 'App\Models\User',

    /*
     * Name of the foreign key column linking user data to other models.
     */
    'foreign_key' => 'user_id',

    /*
     * Name of the table storing user data.
     */
    'table' => 'users',

    /*
     * Defines the authentication guards used for verifying the user.
     * Multiple guards can be specified for flexible authentication strategies.
     * Ensure these guards are configured correctly in the 'guards' section of the auth.php config file.
     */
    'guards' => ['web'],

    /*
     * Specify the type of foreign key being used (e.g., 'id', 'uuid', 'ulid').
     * For non-standard IDs, make sure to add the relevant traits to your models.
     */
    'foreign_key_type' => 'id', // Options: uuid, ulid, id

    /*
     * Attribute of the user model used to display the user's name.
     * If you wish to use a different attribute (e.g., username), change this value accordingly.
     */
    'display_attribute' => 'name',
],

'user' => [
    ...

    /*
     * Specify the type of foreign key being used (e.g., 'id', 'uuid', 'ulid').
     * For non-standard IDs, make sure to add the relevant traits to your models.
     */
    'foreign_key_type' => 'id', // Options: uuid, ulid, id
],

protected $middlewareGroups = [
    'web' => [
        ...
        \Binafy\LaravelUserMonitoring\Middlewares\VisitMonitoringMiddleware::class,
    ],

    'api' => [
        // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

'visit_monitoring' => [
    /*
     * List of pages that should be excluded from visit monitoring.
     * Add route names or URL paths to this array if you want to exclude certain pages.
     */
    'except_pages' => [
        'user-monitoring/visits-monitoring',
        'user-monitoring/actions-monitoring',
        'user-monitoring/authentications-monitoring',
    ],
],

'visit_monitoring' => [
    ...

    /*
     * Set the number of days after which visit records should be automatically deleted.
     * Set to 0 to disable automatic deletion.
     *
     * To enable automatic deletion, configure Laravel's task scheduling as outlined here:
     * https://laravel.com/docs/scheduling
     */
    'delete_days' => 0,
],



namespace App\Console;

...
use Binafy\LaravelUserMonitoring\Commands\RemoveVisitMonitoringRecordsCommand;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
         $schedule->command(RemoveVisitMonitoringRecordsCommand::class)->hourly();
    }
}

'visit_monitoring' => [
    ...

    /*
     * Enable or disable the visit monitoring feature.
     * Set false to disable tracking of user visits.
     */
    'turn_on' => true,
    
    ...
]

'visit_monitoring' => [
    ...

    /*
     * Enable or disable monitoring for AJAX requests.
     * Set to false if you do not wish to track AJAX-based page loads.
     */
    'ajax_requests' => true,

    ...
],



namespace App\Models;

use Binafy\LaravelUserMonitoring\Traits\Actionable;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use Actionable;
}

'action_monitoring' => [
    ...
    
    /*
     * Enable or disable monitoring of specific actions (e.g., store, update, delete).
     * Set to true to monitor actions or false to disable.
     */
    'on_store'      => true,
    'on_update'     => true,
    'on_destroy'    => true,
    'on_read'       => true,
    'on_restore'    => false,
    'on_replicate'  => false,
],

'authentication_monitoring' => [
    ...

    /*
     * Enable or disable monitoring of user login and logout events.
     * Set to true to track these actions, or false to disable.
     */
    'on_login' => true,
    'on_logout' => true,
],

'action_monitoring' => [
    ...

    /*
     * If your application is behind a reverse proxy (e.g., Nginx or Cloudflare),
     * enable this setting to fetch the real client IP from the proxy headers.
     */
    'use_reverse_proxy_ip' => false,

    /*
     * The header used by reverse proxies to forward the real client IP.
     * Common values are 'X-Forwarded-For' or 'X-Real-IP'.
     */
    'real_ip_header' => 'X-Forwarded-For',
],
shell
php artisan vendor:publish --tag="laravel-user-monitoring-config"
shell
php artisan vendor:publish --tag="laravel-user-monitoring-migrations"
shell
php artisan vendor:publish --tag="laravel-user-monitoring-views"
shell
php artisan vendor:publish --tag="laravel-user-monitoring-middlewares"
shell
php artisan vendor:publish --tag="laravel-user-monitoring-routes"
shell
php artisan vendor:publish --provider="Binafy\LaravelUserMonitoring\Providers\LaravelUserMonitoringServiceProvider"
shell
php artisan vendor:publish --tag="laravel-user-monitoring-routes"