PHP code example of gottvergessen / activity

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

    

gottvergessen / activity example snippets



return [
    /*
    |--------------------------------------------------------------------------
    | Activity Logging Enabled
    |--------------------------------------------------------------------------
    |
    | This option controls whether activity logging is enabled globally.
    | You can disable this to turn off all activity logging.
    |
    */
    'enabled' => true,

    /*
    |--------------------------------------------------------------------------
    | Ignore Seeding
    |--------------------------------------------------------------------------
    |
    | When enabled, activity events triggered while running database seeders
    | are skipped.
    |
    */
    'ignore_seeding' => true,

    /*
    |--------------------------------------------------------------------------
    | Activity Logs Table Name
    |--------------------------------------------------------------------------
    |
    | The database table name where activity logs will be stored.
    |
    */
    'table' => 'activity_logs',

    /*
    |--------------------------------------------------------------------------
    | Default Log Category
    |--------------------------------------------------------------------------
    |
    | The default log category to use when a model doesn't specify one.
    |
    */
    'default_log' => 'default',

    /*
    |--------------------------------------------------------------------------
    | Tracked Events
    |--------------------------------------------------------------------------
    |
    | The default events that will be tracked for all models.
    | Models can override this using the $trackEvents property.
    |
    */
    'events' => [
        'created',
        'updated',
        'deleted',
        'restored',
    ],

    /*
    |--------------------------------------------------------------------------
    | Ignored Attributes
    |--------------------------------------------------------------------------
    |
    | Attributes that should be ignored when logging changes globally.
    | Models can add more via the $ignoredAttributes property.
    |
    */
    'ignore_attributes' => [
        'created_at',
        'updated_at',
        'deleted_at',
        'remember_token',
    ],

    /*
    |--------------------------------------------------------------------------
    | Capture Causer
    |--------------------------------------------------------------------------
    |
    | Automatically capture the authenticated user who made the change.
    |
    */
    'capture_causer' => true,

    /*
    |--------------------------------------------------------------------------
    | Capture Request Metadata
    |--------------------------------------------------------------------------
    |
    | Capture HTTP request metadata (method, host).
    | Only applies to web requests, not console commands.
    |
    */
    'capture_request_meta' => false,

    /*
    |--------------------------------------------------------------------------
    | Capture IP Address
    |--------------------------------------------------------------------------
    |
    | Capture the IP address of the user making the change.
    | Only applies to web requests. Requires explicit opt-in due to privacy.
    |
    */
    'capture_ip' => false,

    /*
    |--------------------------------------------------------------------------
    | Auto Batch
    |--------------------------------------------------------------------------
    |
    | Automatically assign a unique batch ID to each request's activities.
    | When enabled, all activities within a single request share a batch_id.
    |
    */
    'auto_batch' => false,

];

use Gottvergessen\Activity\Traits\TracksModelActivity;

class User extends Authenticatable
{
    use TracksModelActivity;
}

$user = User::create(['name' => 'John', 'email' => '[email protected]']);
// ✓ Creates activity log with event='created'

$user->update(['name' => 'Jane']);
// ✓ Creates activity log with event='updated' showing the change

$user->delete();
// ✓ Creates activity log with event='deleted'

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Gottvergessen\Activity\Traits\TracksModelActivity;

class Project extends Model
{
    use TracksModelActivity;

    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }

    public function assignMember(User $user): void
    {
        $this->members()->syncWithoutDetaching([$user->getKey()]);

        $this->logActivity('updated', [
            'relations' => [
                'members' => ['attached' => [$user->getKey()]],
            ],
        ]);
    }

    public function removeMember(User $user): void
    {
        $this->members()->detach($user->getKey());

        $this->logActivity('updated', [
            'relations' => [
                'members' => ['detached' => [$user->getKey()]],
            ],
        ]);
    }
}

use Gottvergessen\Activity\Traits\TracksModelActivity;

class User extends Authenticatable
{
    use TracksModelActivity;

    protected array $ignoredAttributes = [
        'password',
        'remember_token',
    ];

    /*
    * Changes to the listed attributes will not appear in the activity properties
    * If only ignored attributes change, no activity log entry is created
    */
}

config(['activity.ignore_seeding' => true]);

use Gottvergessen\Activity\Traits\TracksModelActivity;
use Gottvergessen\Activity\Traits\InteractsWithActivity;

class User extends Authenticatable
{
    use TracksModelActivity, InteractsWithActivity;
}

// Eager load activities
$users = User::with('activities')->get();

// Get all activities for a user
$user->activities()->get();

// Filter activities
$user->activities()->where('event', 'updated')->get();

// Get the most recent activity
$latest = $user->latestActivity();

// Check if user has any activities
if ($user->hasActivities()) {
    echo "This user has activity history";
}

use Gottvergessen\Activity\Traits\TracksModelActivity;

class User extends Authenticatable
{
    use TracksModelActivity;

    protected array $trackEvents = [
        'created',
        'updated',
    ];

    /*
    * Limit which events are tracked for this model
    */
}

use Gottvergessen\Activity\Traits\TracksModelActivity;

class Invoice extends Model
{
    use TracksModelActivity;

    public function activityLog(): string
    {
        return 'invoices';
    }
}

use Gottvergessen\Activity\Traits\TracksModelActivity;

class Appointment extends Model
{
    use TracksModelActivity;

    public function activityAction(string $event): string
    {
        return match ($event) {
            'created' => 'blog created',
            'updated' => 'blog updated',
            'deleted' => 'cancelled',
        };
    }
}

use Gottvergessen\Activity\Traits\TracksModelActivity;

class Appointment extends Model
{
    use TracksModelActivity;

    public function activityDescription(string $event): string
    {
        return match ($event) {
            'created' => "Appointment was scheduled for {$this->scheduled_at}",
            'updated' => "Appointment has been updated to {$this->new_appointment_date}",
            default   => $event,
        };
    }
}

use Gottvergessen\Activity\Activity;

Activity::batch(function () {
    $user->update(['name' => 'John']);
    $user->profile()->update(['bio' => 'Updated bio']);
    // Both changes share the same batch_id
});

use Gottvergessen\Activity\Support\ActivityContext;

ActivityContext::withoutLogging(function () {
    // These changes won't be logged
    User::create(['name' => 'John']);
    $user->update(['email' => '[email protected]']);
});

// Or manually control logging
ActivityContext::disable();
User::create(['name' => 'Jane']); // Not logged
ActivityContext::enable();
User::create(['name' => 'Bob']); // Logged

use Gottvergessen\Activity\Models\Activity;

// Filter by event type
Activity::forEvent('created')->get();

// Filter by subject model
Activity::forSubject($user)->get();

// Filter by causer
Activity::causedBy($admin)->get();

// Filter by batch
Activity::inBatch($batchId)->get();

// Filter by log category
Activity::inLog('invoices')->get();

// Filter by date range
Activity::betweenDates($startDate, $endDate)->get();

$deletions = Activity::forEvent('deleted')
    ->forSubject($user)
    ->with('causer')
    ->latest()
    ->get();

class Document extends Model
{
    use TracksModelActivity, InteractsWithActivity;
}

@foreach($document->activities()->latest()->get() as $activity)
    {{ $activity->causer?->name }}: {{ $activity->description }}
@endforeach
bash
php artisan activity:install
bash
php artisan migrate