PHP code example of dwi-wijonarko / laravel-activity-logger
1. Go to this page and download the library: Download dwi-wijonarko/laravel-activity-logger 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/ */
dwi-wijonarko / laravel-activity-logger example snippets
use DwiWijonarko\ActivityLogger\Traits\Loggable;
class Post extends Model
{
use Loggable;
// Optional: customize log name
protected $logName = 'posts';
}
// Simple logging
activity()->log('User viewed dashboard');
// With subject model
activity()
->performedOn($post)
->log('Post was published');
// With causer (authenticated user by default)
activity()
->causedBy($user)
->performedOn($post)
->log('Admin featured this post');
// With custom properties
activity()
->performedOn($post)
->withProperties(['custom' => 'data', 'reason' => 'Featured'])
->log('Post was featured');
// With log name (for categorization)
activity()
->inLog('admin')
->performedOn($post)
->log('Post was reviewed');
use DwiWijonarko\ActivityLogger\Models\Activity;
// Get all activities
$activities = Activity::all();
// Get activities for a specific model
$postActivities = Activity::forSubject($post)->get();
// Get activities by a specific user
$userActivities = Activity::forCauser($user)->get();
// Filter by log name
$adminLogs = Activity::inLog('admin')->get();
// Filter by date range
$recentActivities = Activity::between(
now()->subDays(7),
now()
)->get();
// Combine filters
$activities = Activity::forCauser($user)
->inLog('posts')
->between(now()->subMonth(), now())
->latest()
->paginate(20);
$activity = Activity::first();
// Get the user who performed the action
$user = $activity->causer;
// Get the model that was affected
$subject = $activity->subject;
// Get changes (for update events)
$changes = $activity->getChanges();
$oldValues = $activity->getOldValues();
// Access properties
$customData = $activity->properties;
// In your User model
public function activities()
{
return $this->morphMany(Activity::class, 'causer');
}
// In your Post model
public function activities()
{
return $this->morphMany(Activity::class, 'subject');
}
// Usage
$user->activities; // All activities by this user
$post->activities; // All activities on this post
return [
// Enable/disable logging
'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),
// Table name
'table_name' => 'activity_logs',
// Auto-delete logs older than X days
'delete_after_days' => 90,
// Attributes to ignore when logging updates
'ignore_attributes' => [
'password',
'remember_token',
'created_at',
'updated_at',
],
// Log authentication events
'log_auth' => true,
];
class Post extends Model
{
use Loggable;
protected $logName = 'content';
}
// Activities are automatically logged
$post = Post::create(['title' => 'My Post', 'content' => '...']);
// Logs: "Created Post"
$post->update(['title' => 'Updated Title']);
// Logs: "Updated Post" with old and new values
$post->delete();
// Logs: "Deleted Post"
// When admin approves content
activity()
->inLog('admin')
->performedOn($post)
->causedBy($admin)
->withProperties(['status' => 'approved', 'notes' => 'Quality content'])
->log('Content approved by admin');