PHP code example of stealthfirems / laravel-audit-module
1. Go to this page and download the library: Download stealthfirems/laravel-audit-module 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/ */
stealthfirems / laravel-audit-module example snippets
audit('Action be performed', [
'severity' => 'serverity of the action being logged',
'context' => 'Extra content being stored in the log'
]);
$table->bigIncrements('id'); // Primary key for the log entry
$table->string('action')->index(); //Action being performed in the log
$table->string('severity')->index(); // Severity level of the log | default is 'info'
$table->longText('context')->nullable()->default(null); // Contextual information about the action
$table->string('type')->; //User or system action | System by default unless user is specified
$table->unsignedBigInteger('user_id')->index()->nullable(); // ID of the user performing the action
$table->string('guard')->nullable(); // Guard used for authentication
$table->string('ip_address'); // IP address of the user performing the action
$table->timestamp('created_at')->useCurrent(); // Timestamp of when the log was created
// import our contract & trait
use StealthFireMS\LaravelAuditModule\Contracts\IsAuditableEvent;
use StealthFireMS\LaravelAuditModule\Traits\AuditableEvent;
class MyCustomEvent implements IsAuditableEvent
{
use AuditableEvent;
public function handle()
{
// ToDo: your event logic.
}
// optional - by default will be handled by the AuditableEvent trait
public function getAuditMessage(): string
{
return 'Action performed';
}
// optional - by default will be handled by the AuditableEvent trait
public function getAuditContext(): array
{
return ['more_data' => 'Goes here'];
}
}
use Illuminate\Database\Eloquent\Model;
use StealthFireMS\LaravelAuditModule\Traits\AuditableModel;
class YourModel extends Model
{
use AuditableModel;
/**
* An array of columns that shouldn't be audited.
* @var array|string[]
*/
protected array $excludedFromAuditing = [
'created_at',
'updated_at',
];
}
use StealthFireMS\LaravelAuditModule\Observers\AuditableModelObserver as BaseObserver;
class AuditableObserver extends BaseObserver
{
// your custom classes here
// see https://laravel.com/docs/11.x/eloquent#observers for more information
}