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;

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

$audit = AuditService::init(); #initiate object

#all of this value is optional
$audit->setAction("ADD_NEW_DATA")
      ->setMessage("add new data product with category #digitcal")
      ->setTag(["#product"])
      ->setAdditional(["this could be meta data or something else"])
      ->setObject($product)
      ->setAppName("Product App") #in case you will use this package in multiple project and sharing database, you can defined the record from which app
      ->addBefore("product", null) #data before process, key product is null
      ->addAfter("product", $product); #data after process, the key of product is not null
      ->log(); #you can also write before after here log(["product" => null], ["product" => $product]), first parameter as data before, and second parameter as data after
#if your code in single function, you can just use log, without addAfter and addBefore
#but if your code in separate function, you can use that method to append key of collection
#you can also add Collection, string, array as second parameter of addAfter and addBefore
#
console
php artisan vendor:publish --provider="Iqbalatma\LaravelAudit\Providers\LaravelAuditServiceProvider"
console
php artisan migrate