Skip to content

Repository files navigation

🗂️ Laravel Activity Log

Latest Version on PackagistTotal DownloadsLicense: MITPHP Version

A simple and powerful Activity Log package for Laravel. It automatically tracks and records every user action in your admin or user panel after login — no complex setup needed.

✅ Tested with Laravel 8, 9, 10, 11, 12


✨ Features

  • 🔍 Automatically logs all authenticated user activity
  • 🚫 Ignore specific routes you don't want to track
  • 🗑️ Auto-delete old logs based on configurable time limit
  • 📋 Built-in default view to see all logs at /log
  • 🛠️ Create custom logs manually with full control
  • ⚡ Easy installation via Composer

📋 Requirements

RequirementVersion
PHP>= 7.0
Laravel>= 8.x
Guzzle HTTP^7.0

🚀 Installation

Install the package via Composer:

composer require learncodeweb/activitylog

Register Service Provider

⚠️Important: This package does not support Laravel's auto-discovery. You must manually register the service provider based on your Laravel version.


Laravel 8, 9, 10 — config/app.php

Open config/app.php and add inside the providers array:

/* * Package Service Providers... */Lcw\Activitylog\Providers\ActivityLogProvider::class,

Laravel 11 & 12 — bootstrap/providers.php

Laravel 11 and 12 no longer useconfig/app.php for providers. Instead, open bootstrap/providers.php and add:

<?phpreturn [
App\Providers\AppServiceProvider::class,
Lcw\Activitylog\Providers\ActivityLogProvider::class, // ✅ Add this line
];

🗄️ Database Setup

Step 1 — Publish Migration

php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="migrations"

Step 2 — Run Migration

php artisan migrate

This creates the activity_logs table in your database.


⚙️ Configuration (Optional)

Export the config file to customize settings:

php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="config"

This creates config/lcw_activity_log_config.php. Below are the available options:

<?phpreturn [
/* |-------------------------------------------------------------------------- | Delete Limit |-------------------------------------------------------------------------- | Set how many months of old logs to keep before auto-deletion. | Default: 3 months */'delete_limit' => 3,
/* |-------------------------------------------------------------------------- | Ignore Routes |-------------------------------------------------------------------------- | List route names or URIs where you do NOT want activity to be logged. | Example: dashboard index, file downloads, etc. */'ignore_routes' => [
'dashboard.index',
'settings.download.history.activity_log',
],
];

📖 Usage

View Logs (Default Built-in View)

The package automatically provides a log viewer at:

http://your-domain.com/log

Route name:lcw_activity_log_index


Get Logs in Your Own Controller

You can fetch logs manually and display them in your own view:

useLcw\Activitylog\ActivityLog;
publicfunctionindex(Request$request)
{
$activityLog = newActivityLog();
$logs = $activityLog->get($request);
returnview('your-view', compact('logs'));
}

Delete Old Logs

Delete logs older than a given number of months:

useLcw\Activitylog\ActivityLog;
$activityLog = newActivityLog();
// Delete logs older than 1 month$activityLog->logDelete(1);
// Delete logs older than 3 months$activityLog->logDelete(3);

Create a Custom Log Entry

You can manually create a log entry with custom data:

useLcw\Activitylog\ActivityLog;
$activityLog = newActivityLog();
$activityLog->create([
'log' => 'User updated their notification settings',
'query_string' => $request->all(),
]);

Available Parameters for create()

ParameterTypeDescription
logstringA description of the activity
server_ipstringThe server's IP address
user_ipstringThe client/user's IP address
route_detailarrayArray with current route path details
query_stringarrayArray of request parameters
user_idintAuthenticated user's ID
userarrayArray of authenticated user data
created_atdatetimeCustom timestamp for the log entry

🔒 Ignoring Routes

To skip logging on specific routes, publish the config (see above) and add route names or URIs to the ignore_routes array:

'ignore_routes' => [
'dashboard.index', // by route name'api/health-check', // by URI'settings.download.history.activity_log',
],

📁 Package Structure

src/
├── ActivityLog.php # Core activity log class
├── Providers/
│ └── ActivityLogProvider.php # Service provider
├── Http/
│ └── Middleware/ # Auto-logging middleware
├── Models/
│ └── ActivityLogModel.php # Eloquent model
├── database/
│ └── migrations/ # Migration files
├── config/
│ └── lcw_activity_log_config.php # Config file
└── resources/
└── views/ # Default log viewer blade templates

🧪 Example: Full Controller Usage

<?phpnamespaceApp\Http\Controllers;
useIlluminate\Http\Request;
useLcw\Activitylog\ActivityLog;
class ActivityLogController extends Controller
{
protected$activityLog;
publicfunction__construct()
{
$this->activityLog = newActivityLog();
}
// Show all activity logspublicfunctionindex(Request$request)
{
$logs = $this->activityLog->get($request);
returnview('admin.activity-log', compact('logs'));
}
// Delete logs older than X monthspublicfunctiondeleteOld(Request$request)
{
$months = $request->input('months', 3);
$this->activityLog->logDelete($months);
returnback()->with('success', 'Old logs deleted successfully!');
}
// Manually log a custom actionpublicfunctionlogCustomAction()
{
$this->activityLog->create([
'log' => 'Admin exported the user report',
]);
returnback()->with('success', 'Action logged!');
}
}

🏢 Multi-Tenant Setup (stancl/tenancy)

Agar aap stancl/tenancy (tenancy-for-laravel) use kar rahe hain toh neeche diye steps follow karo. Is package ki migration tenant database mein hogi aur har tenant apna alag activity log rakhega.


Step 1 — Migration Publish Karke Tenant Folder Mein Move Karo

stancl/tenancy mein tenant migrations database/migrations/tenant/ folder mein honi chahiye.

# Pehle publish karo
php artisan vendor:publish --provider="Lcw\Activitylog\Providers\ActivityLogProvider" --tag="migrations"# Phir tenant folder mein move karo
mv database/migrations/*_create_activity_log_master.php database/migrations/tenant/

Step 2 — Tenancy Config Mein Migration Path Confirm Karo

config/tenancy.php mein check karo ke tenant migrations ka path sahi set hai:

'migration_parameters' => [
'--path' => ['/database/migrations/tenant'],
'--realpath' => true,
'--force' => true,
],

Step 3 — Tenant Migration Run Karo

# Sab tenants par migrate karo
php artisan tenants:migrate
# Sirf ek specific tenant par migrate karo
php artisan tenants:migrate --tenants=your-tenant-id

Yeh command har tenant ke alag database mein activity_log_master table create kar degi.


Step 4 — Naye Tenant Par Auto Migration

Jab naya tenant create ho toh automatically migration run ho — app/Providers/TenancyServiceProvider.php mein confirm karo ke yeh configured hai:

useStancl\Tenancy\Events;
useStancl\Tenancy\Jobs\MigrateDatabase;
'listeners' => [
Events\TenantCreated::class => [
MigrateDatabase::class, // ✅ Naye tenant par auto migrate hoga
],
],

Agar yeh set hai toh har naye tenant ke create hone par activity_log_master table automatically ban jayega.


Step 5 — Tenant Context Mein Normal Use Karo

stancl/tenancy automatically tenant ka database switch karta hai. Package bilkul normal tarah use karo — har tenant ka data automatically uske apne database mein jayega:

useLcw\Activitylog\ActivityLog;
$activityLog = newActivityLog();
// Logs fetch karo (current tenant ka database use hoga automatically)$logs = $activityLog->get($request);
// Custom log banana$activityLog->create([
'log' => 'User ne invoice download ki',
]);
// Purane logs delete karo$activityLog->logDelete(3);

Step 6 — Central App Se Specific Tenant Ka Log Dekhna

Agar aap central panel se kisi specific tenant ka log access karna chahte hain:

useLcw\Activitylog\ActivityLog;
$tenant = \App\Models\Tenant::find('tenant-id');
tenancy()->initialize($tenant);
$activityLog = newActivityLog();
$logs = $activityLog->get($request);
tenancy()->end();
returnview('admin.tenant-logs', compact('logs'));

Tenant Default Log View

Tenant subdomain ya domain par default log view is URL par milega:

http://tenant1.yourdomain.com/log

Har tenant ka /log sirf usi tenant ka data dikhayega.


Tenant Setup — Quick Reference

TaskCommand
Migration publish karophp artisan vendor:publish --tag="migrations"
Migration move karodatabase/migrations/tenant/ folder mein
Sab tenants migrate karophp artisan tenants:migrate
Single tenant migrate karophp artisan tenants:migrate --tenants=id
Tenant rollback karophp artisan tenants:migrate --rollback
Config publish karophp artisan vendor:publish --tag="config"

❓ FAQ

Q: Does it log guest (unauthenticated) users?
A: No, this package is designed for logged-in users only. It works best inside admin/user panels after authentication.

Q: Can I use my own view instead of the default /log page?
A: Yes! Use $activityLog->get($request) in your controller and pass the data to your own Blade view.

Q: How do I stop certain pages from being logged?
A: Publish the config file and add those route names or URIs to the ignore_routes array.

Q: Does it work with Laravel 12?
A: Yes, but you must manually register the provider in bootstrap/providers.php — see the Register Service Provider section above.

Q: Does it support multi-tenancy with stancl/tenancy?
A: Yes! Move the published migration to database/migrations/tenant/, run php artisan tenants:migrate, and the package will work per-tenant automatically. See the Multi-Tenant Setup section for full details.


🤝 Contributing

Contributions, issues, and feature requests are welcome!
Feel free to open an issue or submit a pull request.


📄 License

This package is open-sourced software licensed under the MIT License.


👨‍💻 Author

Mian Zaid (Khalid Zaid)
🌐 quran.ahlesunat.com
📧 zaidbinkhalid31@gmail.com
📦 Packagist | GitHub


If this package helped you, please consider giving it a ⭐ on GitHub!

About

save all dashboard panel activity

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages