PHP code example of souravmsh / audit-trail

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

    

souravmsh / audit-trail example snippets




use Souravmsh\AuditTrail\Traits\AuditTrail;

class User extends Authenticatable
{
    use AuditTrail;

    /**
     * Define attributes to be ignored in auditing.
     * This will merge with the default ignored attributes in `config/audit-trail.php`.
     * If you don't need to ignore any attributes, you can omit this property.
     */
    public $auditTrailIgnoreAttributes = ['email'];
    
    /**
     * Define specific attributes to track.
     * If this is left blank, all attributes will be tracked except the ignored ones.
     */
    public $auditTrailAllowedAttributes = ['name', 'email', 'about'];
}

return [
    /*
     * Enable or disable activity logging.
     * You can manage it using the `.env` file with `AUDITTRAIL_ENABLED=true/false`.
     */
    'enabled' => env('AUDITTRAIL_ENABLED', true),

    /*
     * Configuration for the audit trail table.
     */
    'migration' => [
        'table' => 'audit_trails',  // The name of the table that stores the activity logs
        'pagination' => 10,  // Number of activities to display per page
    ],

    /*
     * Default attributes to be ignored during activity logging.
     * This prevents logging of sensitive fields.
     */
    'ignored_attributes' => [
        '_token', 'remember_token', 'password', 'created_at', 'updated_at', 'deleted_at',
    ],
];

use Souravmsh\AuditTrail\Facades\AuditTrail;
        
// Supports both array-based calls
AuditTrail::log([
    "type"       => "CREATED",
    "message"    => "A recond has created",
    "model_type" => "App\Models\User",
    "model_id"   => 1,
    "data"       => ["title" => ["old" => "John", "new" => "Alex"]],
]);

// Handles loggedin without a model
AuditTrail::log([
    "type"    => config('audit-trail.migration.type.loggedin'),
    "message" => "A user login in 1."
]);

// or
AuditTrail::log("LOGGEDIN", "A user logged in 2.");

use Souravmsh\AuditTrail\Facades\AuditTrail;

$auditLogs = AuditTrail::history([
    "per_page"     => "10",
    "limit"        => "100",
    "model_type"   => "App\Models\User",
    "model_id"     => "1",
    "creator_type" => "",
    "creator_id"   => "",
    "show_model"   => "false",
    "show_creator" => "true"
]);
bash
php artisan audit-trail:install