PHP code example of uatthaphon / laravel-activity-monitor

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

    

uatthaphon / laravel-activity-monitor example snippets


use AMLog;

$post = Post::where('user_id', $id)->firstOrFail();
$post->body = 'update body content';
$post->save();

AMLog::logName('custom log name')               // Declare log name
    ->description('user updated post content')  // Log description
    ->happenTo($post)                           // Model of the event happen to
    ->actBy(\Auth::user())                      // Model that cause this event
    ->meta(['key'=>'value'])                    // Additional pieces of information
    ->save();                                   // Let's Save the log

use AMLog;
...
AMLog::debug('some debug description')->save();

AMLog::error('some error description')->save();

AMLog::fatal('some fatal description')->save();

AMLog::info('some info description')->save();

AMLog::warning('some warning description')->save();

namespace App\Models;

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;
    ...
}


...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $loggable = ['title', 'description']
}

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $eventsToLog = ['created']
}

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $createdEventMeta = ['create key' => 'create value'];

    protected static $updatedEventMeta = ['update key' => 'update value'];

    protected static $deletedEventMeta = ['deletd key' => 'delete value'];

    ...
}

use AMView;

...

// Get all
AMView::all();                                    // get all the logs
AMView::get();                                    // also act the same as all()

// With conditions
AMView::logName('your_log_name')                  // get by log name
    ->limit(5)                                    // limit resutls
    ->sortBy('desc')                              // sort By desc or asc
    ->get();

// Get from multiple log names
AMView::logName('info', 'updated')->get();
AMView::logName(['info', 'updated'])->get();


// Get all specific lastest post log From current user
$user = \Auth::user();
$post = $user->post()->last($user);
AMView::happenTo($post)->ActBy($user)->get();

// Or call from providings log name function
AMView::debug()->all();

AMView::error()->all();

AMView::fatal()->all();

AMView::info()->all();

AMView::warning()->all();

...

use AMView;

...

$am = AMView::info()->all()->last();

$am->log_name;                          // Get log name
$am->description;                       // Get description
$am->agent;                             // Get user browser agent
$am->ip;                                // Get user ip address

$traces = $am->traces;                  // Get traces

foreach ($traces as $key => $value) {
    // do something
}

$meta = $am->meta;                      // Get you custom meta data

foreach ($meta as $key => $value) {
    // do something
}

...

...
use Uatthaphon\ActivityMonitor\Traits\ActivityMonitorRelations;

class User extends Authenticatable
{
  use ActivityMonitorRelations;

  ...
}

// Get all activity records of the current user
\Auth::user()->activity()->get();

// Retrived with more specific
// By tell with model record user was interact with
$user = \Auth::user();
$user->activity()->happenTo($user->posts()-last())->get();

// Use the providing log name function
$user->activity()->info()->get();

// Use the providing log name with specific fom model togetter
$user->activity()
    ->info()
    ->happenTo($user->posts()-last())
    ->get();
bash
php artisan vendor:publish --tag=migrations
bash
php artisan migrate