PHP code example of msonowal / laravel-auditor

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

    

msonowal / laravel-auditor example snippets


[

    /*
     * If set to false, no activities will be saved to the database.
     */
    'enabled' => env('AUDIT_ENABLED', true),

    /*
     * By default all the activities will be processed via queue
     * If set to false, all the activities will be processed instantly.
     */
    'use_queue' => env('AUDIT_MODE', true),

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified here will be deleted.
     */
    'delete_records_older_than_days' => 365,

    /*
     * When the clean-command is executed, all recording activities older than
     * the number of days specified above and if its beyond the max entries limit
     * those records will be deleted and mostly on the specified log name
     */
    'max_entries' => 50000,

    /*
     * If no log name is passed to the audit() helper
     * we use this default log name.
     */
    'default_log_name' => env('AUDIT_DEFAULT_LOG_NAME', 'default'),

    /*
     * If set to true, the subject returns soft deleted models.
     */
    'subject_returns_soft_deleted_models' => true,

    /*
     * This is the name of the database connection that will be used by the migration and
     * used by the Services.
     */
    'connection_name' => env('AUDIT_CONNECTION', 'activity'),
    /*
     * This is the name of the collection that will be created by the migration and
     * used by the AuditActivity model.
     */
    'collection_name' => env('AUDIT_COLLECTION_NAME', 'activity_logs'),
];

audit()->log('Something, has been done');

function test(AuditServiceRepository $audit)
{
    $audit->all(); //this returns all the records in plain array directly from DB

    $audit->paginate(); //this returns all the records as a Model Instance with default 50 per page and all the fields
}

audit()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->add('Something, has been done');
   
$lastLoggedActivity = AuditActivityMoloquent::all()->last();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue'
$lastLoggedActivity->description; //returns 'Look, I logged something'




namespace Msonowal\Audit\Tests\Unit;

use Msonowal\Audit\Tests\TestCase;

class ExampleTest extends TestCase
{
    /** @test */
    public function example_test_method()
    {
        $this->assertTrue(true);
    }
}

bash
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Msonowal\Audit\AuditServiceProvider" --tag="migrations"
bash
php artisan migrate