PHP code example of iqbalatma / laravel-audit

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

    

iqbalatma / laravel-audit example snippets




use Iqbalatma\LaravelAudit\AuditService;
use App\Models\Product;
use Illuminate\Support\Facades\Auth;

$product = Product::create([
  "name"     => "laptop",
  "category" => "digital",
  "price"    => 10000000
]);

$audit = AuditService::init(); #initiate object
#or
$audit = AuditService::init(action: "CREATE_PRODUCT", message: "Create product via method abc in class xyz", entryObject: $product, guard: "web", user: Auth::user());

#all of this value is optional
AuditService::init("CREATE_PRODUCT", "Create product via method abc in class xyz")
    ->setEntryObject($product) #   ->toArray();

$role->permissions()->sync($requestedData['permissions'] ?? []);
$role->load('permissions');

$permissionAfter = $role->permissions
    ->map(fn($p) => $p->only(['id', 'name']))
    ->toArray();

$audit->addRelationalTrail(Table::PERMISSIONS->value, $permissionBefore, $permissionAfter);

$audit->execute();
console
php artisan vendor:publish --provider="Iqbalatma\LaravelAudit\Providers\LaravelAuditServiceProvider"
console
php artisan migrate