PHP code example of arpanihan / auditify

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

    

arpanihan / auditify example snippets


return [
    // Activity logging options
    'activity' => [
        'enabled' => true,
        'events' => [
            'page_visit' => true,
            'tab_switch' => true,
            'modal_open' => true,
            'button_click' => true,
        ],
        'js_path' => '/vendor/auditify/js/activityLog.js',
    ],

    // Action logging options
    'action' => [
        'enabled' => true,
        'modules' => ['user', 'product', 'order', 'report'],
    ],

    // Database settings
    'database' => [
        'check_existing_tables' => true,
        'use_foreign_keys' => false,
    ],

    // Table names (customize to avoid conflicts)
    'tables' => [
        'activity_logs' => env('AUDITIFY_ACTIVITY_TABLE', 'auditify_activity_logs'),
        'action_logs'   => env('AUDITIFY_ACTION_TABLE', 'auditify_action_logs'),
    ],

    // Pagination and retention
    'pagination' => [
        'activity_log_limit' => 50,
        'action_log_limit'   => 50,
    ],
    'retention_days' => 365,
];

return [

    /*
    |--------------------------------------------------------------------------
    | Activity Log Options
    |--------------------------------------------------------------------------
    |
    | Options for ActivityLogger. External users can enable/disable certain events.
    | Example: Page visits, Tab switches, Modal opens, Button clicks.
    |
    */
    'activity' => [
        'enabled' => true, // Enable/Disable activity logging
        'events' => [
            'page_visit' => true, // Log page visits
            'tab_switch' => true,  // Log tab switches
            'modal_open' => true,  // Log modal openings
            'button_click' => true, // Log button clicks
        ],
        'js_path' => '/vendor/auditify/js/activityLog.js', // Path for the frontend activity logger JS file
    ],

    /*
    |--------------------------------------------------------------------------
    | Action Log Options
    |--------------------------------------------------------------------------
    |
    | Options for ActionLogger. Mainly for CRUD/system operations.
    | You can enable/disable specific modules if needed.
    |
    */
    'action' => [
        'enabled' => true, // Enable/Disable action logging
        'modules' => [
            'user', 'product', 'order', 'report', // Default modules for logging (can be modified)
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | General Options
    |--------------------------------------------------------------------------
    |
    | Default values for table names, pagination, and log retention.
    |
    */
    'tables' => [
        'activity_logs' => 'activity_logs', // Table name for activity logs
        'action_logs'   => 'action_logs',   // Table name for action logs
    ],

    'pagination' => [
        'activity_log_limit' => 50, // Default limit for activity log pagination
        'action_log_limit'   => 50, // Default limit for action log pagination
    ],

    'retention_days' => 365, // Optionally auto-delete older logs after this number of days
];

use ArpaNihan\Auditify\Services\ActionLogger;

// Basic usage
ActionLogger::log('created', 'User created a new post', null, 'Posts');

// With all parameters
ActionLogger::log(
    action: 'updated',
    message: 'Post title was updated',
    url: request()->fullUrl(),
    module: 'Posts',
    user_id: auth()->id(),
    user_name: auth()->user()->name,
    user_role: auth()->user()->role
);

// Examples of different actions
ActionLogger::log('login', 'User logged in successfully', null, 'Authentication');
ActionLogger::log('deleted', 'Product #123 was deleted', null, 'Products');
ActionLogger::log('exported', 'Generated sales report', null, 'Reports');

use ArpaNihan\Auditify\Services\ActivityLogger;

// Basic usage
ActivityLogger::log('page_visit', 'User visited dashboard', null, 'Dashboard');

// Common activity examples
ActivityLogger::log('tab_switch', 'Switched to settings tab', null, 'Settings');
ActivityLogger::log('modal_open', 'Opened user profile modal', null, 'Users');

use ArpaNihan\Auditify\Facades\Auditify;

// Using facade (registers as 'auditify' service)
app('auditify')->log('created', 'New user registered', null, 'Users');
app('activitylog')->log('page_visit', 'Visited profile page', null, 'Profile');

use ArpaNihan\Auditify\Services\ActionLogger;

public function test_action_logging_works()
{
    ActionLogger::log('test_action', 'Testing action logging', null, 'Tests');
    
    $this->assertDatabaseHas('auditify_action_logs', [
        'action' => 'test_action',
        'message' => 'Testing action logging',
        'module' => 'Tests'
    ]);
}

use ArpaNihan\Auditify\Services\ActivityLogger;

ActivityLogger::log(
    'visited',  // Action (e.g., 'visited', 'clicked', etc.)
    'User visited the dashboard',  // Message
    'http://example.com/dashboard',  // URL (optional)
    'DashboardModule',  // Module name (optional)
);

// The package will automatically try to resolve roles if App\Models\Role exists
// No additional configuration needed
bash
# Check if everything is properly installed
php artisan auditify:status
bash
php artisan migrate  # This runs ALL migrations, can be dangerous!
bash
php artisan auditify:install  # Safe installation
bash
php artisan migrate --path=/database/migrations/your_auditify_migration_folder
bash
php artisan migrate
bash
php artisan vendor:publish --tag=auditify-config
# Or use the complete installation:
php artisan auditify:install
bash
php artisan vendor:publish --tag=auditify-assets
# Or check installation status:
php artisan auditify:status
bash
# Run package-specific tests
php artisan test tests/Feature/AuditifyIntegrationTest.php

# Or create your own tests
php artisan make:test AuditifyCustomTest