🍲dfcv🏰dd⋉(● ∸ ●)⋊@% PNG %k25u25%fgd5n! PNG %k25u25%fgd5n!home/addsolutions/public_html/ORIENTAL_PACKAGING/myDoc_AuditLog.md000066600000015674152427674030020507 0ustar00Audit Log System - Implementation Guide Overview This audit log system automatically tracks all database changes (create, update, delete) across your Laravel application and provides a comprehensive searchable interface to view and restore deleted records. Files to Copy to New Project 1. Database Migration text database/migrations/xxxx_xx_xx_create_audit_logs_table.php 2. Models text app/Models/AuditLog.php 3. Traits text app/Traits/Auditable.php 4. Controllers text app/Http/Controllers/AuditLogController.php 5. Helpers (Optional - for serial numbers) text app/Helpers/SerialHelper.php 6. Views text resources/views/audit_logs/index.blade.php resources/views/audit_logs/show.blade.php resources/views/audit_logs/partials/rows.blade.php 7. Routes text routes/web.php (add audit log routes) Step-by-Step Implementation Step 1: Run Migration bash php artisan migrate Step 2: Add Auditable Trait to Models You Want to Track Add to any model you want to audit: php use App\Traits\Auditable; class YourModel extends Model { use Auditable; // Add this line } Recommended models to track: PaymentVoucher.php GoodsReceivedNote.php Invoice.php PurchaseOrder.php SalesReturn.php StockTransfer.php Step 3: Add Routes Add to routes/web.php: php Route::middleware(['auth'])->group(function () { Route::get('/audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index'); Route::get('/audit-logs/filter', [AuditLogController::class, 'filter'])->name('audit-logs.filter'); Route::get('/audit-logs/{id}', [AuditLogController::class, 'show'])->name('audit-logs.show'); Route::get('/audit-logs/{id}/advanced-data', [AuditLogController::class, 'advancedData']); Route::post('/audit-logs/{id}/restore', [AuditLogController::class, 'restore']); }); Step 4: Add Navigation Link Add to your main layout file (resources/views/layouts/app.blade.php): blade Step 5: Add Dashboard Card (Optional) Add to your dashboard: blade
Audit Logs
System Audit Trail

Track all database changes and user activities.

View Logs
What Gets Tracked Automatically When you add use Auditable to a model, Laravel automatically logs: Action When it happens What's stored Created When a new record is inserted New data as JSON Updated When a record is modified Old data + New data + Changed fields Deleted When a record is deleted Full record data before deletion Restored When a soft-deleted record is restored Restored data Database Table Structure The audit_logs table stores: Column Description table_name Which table was changed (grn, invoices, etc.) record_id ID of the changed record action created, updated, deleted, restored old_data Previous data (JSON) new_data New data (JSON) changed_fields Only fields that changed (JSON) user_id/name/email Who made the change ip_address User's IP address amount Transaction amount (for reports) reference_no Invoice/voucher number Features You Get ✅ Automatic Tracking No extra code needed - just add the trait Captures all CRUD operations ✅ Search & Filter Date range filtering Table name filtering Action type filtering (created/updated/deleted) Keyword search (reference numbers, users, amounts) ✅ Advanced View Modal Shows complete record details Displays serial numbers (if configured) Raw JSON data for debugging ✅ Restore Functionality One-click restore for deleted records Recovers full record data ✅ Export to CSV Export filtered results Useful for reporting and auditing Configuration Options Customize Which Data to Track In your Auditable.php trait, modify the extractRelatedData method to add business-specific data: php protected static function extractRelatedData($model, $action) { $related = []; if ($model instanceof PaymentVoucher) { $related['supplier_name'] = $model->supplier->name; $related['amount'] = $model->total_amount; } return $related; } Add Serial Number Tracking If you have serial numbers in your application, update SerialHelper.php with your table mappings: php protected static $serialMappings = [ 'grn' => [ 'table' => 'item_serials', 'foreign_key' => 'doc_id', 'serial_column' => 'serial_number' ], // Add more tables as needed ]; Testing Checklist After implementation, verify: Run php artisan migrate successfully Create a test record - check if appears in audit log Update a test record - verify old/new data captured Delete a test record - confirm deletion logged Visit /audit-logs - page loads with data Test search/filter functionality Click "Advanced View" on any log Test restore function on a deleted record Check export CSV feature Common Issues & Solutions Issue Solution restored() event error Remove or comment out the restored event in Auditable.php if not using soft deletes No data showing Ensure models have use Auditable trait added Serial numbers not showing Update SerialHelper.php with correct table/column mappings jQuery errors Use pure JavaScript as shown in the view file Slow performance Add indexes to created_at, table_name, action columns Estimated Implementation Time Task Time Copy files 5 minutes Run migration 2 minutes Add trait to models 5-10 minutes Add routes 2 minutes Add navigation link 2 minutes Testing 10 minutes Total ~30 minutes Files Location Summary text your-project/ ├── app/ │ ├── Http/ │ │ └── Controllers/ │ │ └── AuditLogController.php │ ├── Models/ │ │ └── AuditLog.php │ ├── Traits/ │ │ └── Auditable.php │ └── Helpers/ │ └── SerialHelper.php (optional) ├── database/ │ └── migrations/ │ └── xxxx_xx_xx_create_audit_logs_table.php ├── resources/ │ └── views/ │ └── audit_logs/ │ ├── index.blade.php │ ├── show.blade.php │ └── partials/ │ └── rows.blade.php └── routes/ └── web.php (add routes) Support If you encounter any issues: Check Laravel logs: storage/logs/laravel.log Verify database connection Ensure all migrations ran successfully Clear cache: php artisan cache:clear and php artisan view:clear This audit system works with Laravel 8, 9, 10, 11, and 12.