PHP code example of ayup-creative / event-log

1. Go to this page and download the library: Download ayup-creative/event-log 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/ */

    

ayup-creative / event-log example snippets


return [
    /*
     * The model used to represent users in your application.
     * This is used for the 'causer' relationship.
     */
    'user_model' => \App\Models\User::class,

    /*
     * The Eloquent models used for event logs and their relations.
     * You can extend these to add your own logic or relationships.
     */
    'event_model' => \AyupCreative\EventLog\Models\EventLog::class,
    'relation_model' => \AyupCreative\EventLog\Models\EventLogRelation::class,

    /*
     * The name of the queue that event log jobs should be sent to.
     * It is recommended to use a lower priority queue.
     */
    'queue' => 'event-log',
];

use function AyupCreative\EventLog\log_event;

// Basic event
log_event('organisation.created', $organisation);

// Event with related models
log_event('user.enrolled', $user, [$organisation, $course]);

// Event with additional metadata (e.g., tracking reasons, API errors)
log_event('payment.failed', $payment, metadata: [
    'error_reason' => 'Insufficient funds',
    'provider' => 'Stripe'
]);

use function AyupCreative\EventLog\log_event;
use App\Enums\EventName;

log_event(EventName::ORGANISATION_CREATED, $organisation);

use AyupCreative\EventLog\Features\LogsEvents;
use Illuminate\Database\Eloquent\Model;

class Mandate extends Model
{
    use LogsEvents;
}

class Mandate extends Model
{
    use LogsEvents;

    // Change the dot-notation prefix (defaults to snake_case of class name)
    public function eventNamespace(): string
    {
        return 'billing.mandate';
    }

    // Filter which events should be logged
    public function shouldLogEvent(string $event): bool
    {
        return $event !== 'mandate.updated';
    }

    // Attach related models to automatic lifecycle events
    public function eventRelations(string $event): array
    {
        return [$this->organisation];
    }

    // Include automatic metadata in lifecycle logs
    public function eventMetadata(string $event): array
    {
        return ['type' => $this->type];
    }
}

use AyupCreative\EventLog\Facades\EventLog;

public function boot()
{
    // Resolve the current actor ID (e.g., from a custom auth system)
    EventLog::resolveActorWith(function ($app) {
        return auth('api')->id();
    });

    // Determine the type of causer based on context
    EventLog::determineCauserTypeWith(function ($app) {
        if ($app->runningInConsole()) {
            return 'cron';
        }
        return 'user';
    });
}

use AyupCreative\EventLog\Support\WithEventTransaction;

WithEventTransaction::run(function () use ($user, $org) {
    $org->save();
    $user->organisations()->attach($org);

    \AyupCreative\EventLog\log_event('organisation.created', $org);
    \AyupCreative\EventLog\log_event('user.enrolled', $user, [$org]);
});

// app/Http/Kernel.php or bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->append(\AyupCreative\EventLog\Http\Middleware\EventCorrelationMiddleware::class);
})

use Illuminate\Support\Facades\Http;

Http::withEventContext()->post('https://api.other-service.com/data');

use AyupCreative\EventLog\Facades\EventLog;

public function boot()
{
    EventLog::formatEventsWith(function ($eventLog) {
        return match ($eventLog->event) {
            'user.created' => "User {$eventLog->subject->name} joined the platform",
            'payment.failed' => "Payment failed: {$eventLog->meta->error_reason}",
            default => $eventLog->event,
        };
    });
}

namespace App\Support;

class MyEventFormatter
{
    public function __invoke($eventLog)
    {
        // Custom logic to return a human-readable string
        return "Action: " . $eventLog->event;
    }
}

'event_formatter' => \App\Support\MyEventFormatter::class,

$eventLog = EventLog::getFor($user)->first();
echo $eventLog->description; // "User John Doe joined the platform"

use AyupCreative\EventLog\Facades\EventLog;

// Get all events for a model
$events = EventLog::getFor($organisation);

foreach ($events as $log) {
    echo "{$log->description} caused by {$log->causerLabel()}";
    
    // Access metadata
    echo $log->meta->error_reason;
}

// Paginated version
$paginatedEvents = EventLog::getForPaginated($organisation);

echo $log->meta->error_reason;
bash
php artisan vendor:publish --tag="event-log-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="event-log-config"