11# Laravel Audit Log
22
3- A comprehensive entity-level audit logging package for Laravel with support for MySQL and MongoDB databases.
3+ A comprehensive entity-level audit logging package for Laravel with support for MySQL databases.
44
55## Features
66
7- - ✅ ** Multiple Entity Support** : Audit any number of entities with dedicated log tables/collections
8- - ✅ ** Database Drivers ** : Built-in support for MySQL and MongoDB
7+ - ✅ ** Multiple Entity Support** : Audit any number of entities with dedicated log tables
8+ - ✅ ** Database Driver ** : Built-in support for MySQL
99- ✅ ** Model Log Handling** : Automatic tracking of model changes (create, update, delete, restore)
1010- ✅ ** Field Inclusion/Exclusion** : Fine-grained control over which fields to audit
1111- ✅ ** Causer Identification** : Automatic tracking of who made the changes
12- - ✅ ** Auto-Migration** : Automatic table/collection creation for new entities
12+ - ✅ ** Auto-Migration** : Automatic table creation for new entities
1313- ✅ ** SOLID Principles** : Clean, maintainable, and extensible architecture
1414
1515## Requirements
1616
1717- PHP >= 8.2
1818- Laravel 11.x or 12.x
19- - MySQL 8.0+ (for MySQL driver)
20- - MongoDB 4.4+ (for MongoDB driver, optional)
19+ - MySQL 8.0+
2120
2221## Installation
2322
@@ -37,21 +36,13 @@ php artisan vendor:publish --tag=audit-logger-config
3736
3837This will create a ` config/audit-logger.php ` configuration file.
3938
40- ### Step 3: Optional - Install MongoDB Support
41-
42- If you plan to use MongoDB as a driver, install the MongoDB Laravel package:
43-
44- ``` bash
45- composer require mongodb/laravel-mongodb
46- ```
47-
4839## Configuration
4940
5041The configuration file (` config/audit-logger.php ` ) allows you to customize various aspects of the package:
5142
5243``` php
5344return [
54- // Default driver: 'mysql' or 'mongodb'
45+ // Default driver: 'mysql'
5546 'default' => env('AUDIT_DRIVER', 'mysql'),
5647
5748 // Driver configurations
@@ -61,11 +52,6 @@ return [
6152 'table_prefix' => env('AUDIT_TABLE_PREFIX', 'audit_'),
6253 'table_suffix' => env('AUDIT_TABLE_SUFFIX', '_logs'),
6354 ],
64- 'mongodb' => [
65- 'connection' => env('AUDIT_MONGODB_CONNECTION', 'mongodb'),
66- 'collection_prefix' => env('AUDIT_COLLECTION_PREFIX', 'audit_'),
67- 'collection_suffix' => env('AUDIT_COLLECTION_SUFFIX', '_logs'),
68- ],
6955 ],
7056
7157 // Auto-migration for new entities
@@ -86,6 +72,51 @@ return [
8672];
8773```
8874
75+ ### Helper Methods for Retrieving Logs
76+
77+ You can add helper methods to your models to make retrieving audit logs easier:
78+
79+ ``` php
80+ /**
81+ * Get the audit logs for this model.
82+ */
83+ public function auditLogs(array $options = [])
84+ {
85+ return AuditLogger::getLogsForEntity(
86+ entityType: static::class,
87+ entityId: $this->getKey(),
88+ options: $options
89+ );
90+ }
91+
92+ /**
93+ * Get the most recent audit logs for this model.
94+ */
95+ public function recentAuditLogs(int $limit = 10)
96+ {
97+ return AuditLogger::getLogsForEntity(
98+ entityType: static::class,
99+ entityId: $this->getKey(),
100+ options: [
101+ 'limit' => $limit,
102+ 'from_date' => now()->subDays(30)->toDateString(),
103+ ]
104+ );
105+ }
106+
107+ /**
108+ * Get audit logs for a specific action.
109+ */
110+ public function getActionLogs(string $action)
111+ {
112+ return AuditLogger::getLogsForEntity(
113+ entityType: static::class,
114+ entityId: $this->getKey(),
115+ options: ['action' => $action]
116+ );
117+ }
118+ ```
119+
89120## Basic Usage
90121
91122### Making Models Auditable
@@ -290,63 +321,6 @@ Register it in the config:
290321],
291322```
292323
293- ### Using Different Drivers
294-
295- You can use different drivers for different operations:
296-
297- ``` php
298- // Use MongoDB driver for specific operation
299- AuditLogger::driver('mongodb')->log(...);
300-
301- // Use MySQL driver
302- AuditLogger::driver('mysql')->log(...);
303- ```
304-
305- ### Helper Methods for Retrieving Logs
306-
307- You can add helper methods to your models to make retrieving audit logs easier:
308-
309- ``` php
310- /**
311- * Get the audit logs for this model.
312- */
313- public function auditLogs(array $options = [])
314- {
315- return AuditLogger::getLogsForEntity(
316- entityType: static::class,
317- entityId: $this->getKey(),
318- options: $options
319- );
320- }
321-
322- /**
323- * Get the most recent audit logs for this model.
324- */
325- public function recentAuditLogs(int $limit = 10)
326- {
327- return AuditLogger::getLogsForEntity(
328- entityType: static::class,
329- entityId: $this->getKey(),
330- options: [
331- 'limit' => $limit,
332- 'from_date' => now()->subDays(30)->toDateString(),
333- ]
334- );
335- }
336-
337- /**
338- * Get audit logs for a specific action.
339- */
340- public function getActionLogs(string $action)
341- {
342- return AuditLogger::getLogsForEntity(
343- entityType: static::class,
344- entityId: $this->getKey(),
345- options: ['action' => $action]
346- );
347- }
348- ```
349-
350324## Database Schema
351325
352326### MySQL
@@ -370,10 +344,6 @@ CREATE TABLE audit_products_logs (
370344);
371345```
372346
373- ### MongoDB
374-
375- For MongoDB, collections are created with similar fields and appropriate indexes.
376-
377347## Troubleshooting
378348
379349### Common Issues
@@ -395,11 +365,6 @@ For MongoDB, collections are created with similar fields and appropriate indexes
395365 - Model uses ` Auditable ` trait
396366 - Auditing is enabled on the model instance
397367
398- 3 . ** MongoDB driver issues** : Ensure you've installed the MongoDB package:
399- ``` bash
400- composer require mongodb/laravel-mongodb
401- ```
402-
403368## Development
404369
405370### Testing
0 commit comments