Laravel Trustup Model History – Store model change history

Laravel Trustup Model History is a package designed to store and track changes made to Laravel models. It keeps a detailed history of modifications, helping developers audit changes, debug issues, or monitor how data evolves over time. This package allows you to track which attributes were changed, who made the changes, and when they occurred.

 

Key Features:

  1. Tracks Model Changes: Automatically logs any updates, creations, or deletions of models.
  2. Stores Old and New Values: Keeps a record of the previous and updated values of model attributes.
  3. User Identification: Tracks which user made the change if available.
  4. Customizable: You can configure which models and attributes to track and store in the history.

Installation

To install and use Laravel Trustup Model History, follow these steps:

  1. Require the Package: First, add the package to your project via Composer:
  2. Publish the Configuration and Migrations: After installing the package, publish its configuration and migration files:
    This will create the necessary configuration file and database migration for storing model history.
  3. Configure the Package: The published configuration file allows you to define how the package should behave. You can configure which models to track, which attributes to include or exclude, and more.The configuration file will be available at config/trustup-model-history.php.

 

Usage

To track changes on a model, simply use the TrackChanges trait provided by the package.

  1. Add the Trait to Your Models: Open the model(s) you want to track and add the TrackChanges trait:
  2. Track Specific Actions: By default, the package will track create, update, and delete actions. If you want to track additional or custom actions, you can do so by customizing the package configuration.

 

Storing History Data

When a model is created, updated, or deleted, the package will automatically store a record of the change in the model_history table. This table contains:

  • Model name: The name of the model being tracked.
  • Model ID: The ID of the model that was changed.
  • Changes: The details of the attributes that were modified, including old and new values.
  • User: Optionally, the ID of the user who made the change.
  • Timestamp: The date and time the change occurred.

Retrieving History

You can easily retrieve the history of changes for any model. For example, if you want to get the history of changes made to a Post model:

 

Customizing History Tracking

You can customize how model changes are tracked by adjusting the package configuration:

  • Selectively Track Models: Only track history for specific models.
  • Exclude Attributes: Prevent certain model attributes from being logged (e.g., passwords, tokens).
  • Soft Deletes: Track when models are soft deleted (if your models use soft deletes).

Example configuration file (config/trustup-model-history.php):

 

Displaying History in Views

If you want to show the history of changes on a page, you can pass the history data to your Blade views:

 

Conclusion

Laravel Trustup Model History provides a powerful way to keep track of changes to your models. It’s useful for auditing, debugging, and monitoring purposes in your Laravel applications.

 

Additional Considerations

  • Documentation: For more detailed usage and advanced options, refer to the official Laravel Trustup Model History documentation.
  • Webhooks: The package can be extended to trigger webhooks when model changes occur, useful for integrating with external services.

 

Output:

The Laravel Trustup Model History package operates behind the scenes by logging changes to the database rather than providing direct output like console logs. The output is stored in a database table and can be retrieved and displayed via a view or in a debugging console.

1. Database Table Logs

After changes are made to a tracked model (e.g., Post), the changes are recorded in the model_history table in the database. You can inspect this table using tools like phpMyAdmin, Sequel Pro, or Laravel Tinker.

Example SQL query to inspect the changes:

This will show entries like:

  • model_type: App\Models\Post
  • model_id: 1 (ID of the post that changed)
  • changes: JSON object of old and new values
  • user_id: ID of the user who made the change
  • created_at: Timestamp of when the change occurred

2. Display Output in Views

If you want to display the model change history in your application’s UI, you can pass the history to a Blade view and show it.

Example Controller to Retrieve and Pass the History:

 

Example Blade View (resources/views/post/history.blade.php):

 

Expected Output in the Browser:

When a user visits the page, they would see something like:


Change History for Post: Laravel Trustup Model History

Date Changed By Old Title New Title
2024-09-30 User ID: 1 Laravel Audit Feature Laravel Trustup Model History
2024-09-29 User ID: 2 Initial Post Title Laravel Audit Feature

3. Debug Output in Logs

You can also log the changes in Laravel’s log file using the Log facade:

Example Code:

 

Example Log Output (storage/logs/laravel.log):

Summary of Outputs:

  1. Database Table: You can view the history in the model_history table.
  2. Web View: Display history data in a Blade template.
  3. Logs: Log the changes in the laravel.log file for debugging.

 

  1. Laravel Breeze – Simple authentication starter kit
  2. Laravel Jetstream – Scaffolding for Laravel apps
  3. Laravel Passport – API authentication via OAuth2
  4. Laravel Sanctum – Simple API authentication
  5. Spatie Laravel Permission – Role and permission management
  6. Laravel Cashier – Subscription billing with Stripe
  7. Laravel Scout – Full-text search using Algolia
  8. Laravel Socialite – OAuth authentication (Google, Facebook, etc.)
  9. Laravel Excel – Excel import and export for Laravel
  10. Laravel Horizon – Redis queues monitoring
  11. Laravel Nova – Admin panel for Laravel
  12. Laravel Fortify – Backend authentication for Laravel
  13. Laravel Vapor – Serverless deployment on AWS
  14. Laravel Telescope – Debugging assistant for Laravel
  15. Laravel Dusk – Browser testing
  16. Laravel Mix – API for compiling assets
  17. Spatie Laravel Backup – Backup management
  18. Laravel Livewire – Building dynamic UIs
  19. Spatie Laravel Media Library – Manage media uploads
  20. Laravel Excel – Excel spreadsheet handling
  21. Laravel Debugbar – Debug tool for Laravel
  22. Laravel WebSockets – Real-time communication
  23. Spatie Laravel Sitemap – Generate sitemaps
  24. Laravel Spark – SaaS scaffolding
  25. Laravel Envoy – Task runner for deployment
  26. Spatie Laravel Translatable – Multilingual model support
  27. Laravel Backpack – Admin panel
  28. Laravel AdminLTE – Admin interface template
  29. Laravel Collective Forms & HTML – Simplified form and HTML generation
  30. Spatie Laravel Analytics – Google Analytics integration
  31. Laravel Eloquent Sluggable – Automatically create slugs
  32. Laravel Charts – Chart integration
  33. Laravel Auditing – Track changes in models
  34. Laravel JWT Auth – JSON Web Token authentication
  35. Laravel Queue Monitor – Monitor job queues
  36. Spatie Laravel Query Builder – Filter, sort, and include relationships in Eloquent queries
  37. Laravel Datatables – jQuery Datatables API
  38. Laravel Localization – Multilingual support for views and routes
  39. Laravel Acl Manager – Access control list manager
  40. Laravel Activity Log – Record activity in your app
  41. Laravel Roles – Role-based access control
  42. Spatie Laravel Tags – Tagging models
  43. Laravel Installer – CLI installer for Laravel
  44. Laravel Breadcrumbs – Generate breadcrumbs in Laravel
  45. Laravel Mailgun – Mailgun integration for Laravel
  46. Laravel Trustup Model History – Store model change history
  47. Laravel Deployer – Deployment automation tool
  48. Laravel Auth – Custom authentication guards
  49. Laravel CORS – Cross-Origin Resource Sharing (CORS) support
  50. Laravel Notifications – Send notifications through multiple channels
  51. Spatie Laravel Http Logger – Log HTTP requests
  52. Laravel Permission Manager – Manage permissions easily
  53. Laravel Stubs – Customize default stubs in Laravel
  54. Laravel Fast Excel – Speed up Excel exports
  55. Laravel Image – Image processing
  56. Spatie Laravel Backup Server – Centralize backups for Laravel apps
  57. Laravel Forge API – Manage servers through the Forge API
  58. Laravel Blade SVG – Use SVGs in Blade templates
  59. Laravel Ban – Ban/unban users from your application
  60. Laravel API Response – Standardize API responses
  61. Laravel SEO – Manage SEO meta tags
  62. Laravel Settings – Store and retrieve settings
  63. Laravel DOMPDF – Generate PDFs
  64. Laravel Turbo – Full-stack framework for building modern web apps
  65. Spatie Laravel Event Sourcing – Event sourcing implementation
  66. Laravel Jetstream Inertia – Jetstream’s Inertia.js integration
  67. Laravel Envoy Tasks – Task automation
  68. Laravel Likeable – Like/dislike functionality
  69. Laravel GeoIP – Determine visitor’s geographic location
  70. Laravel Country State City – Dropdowns for country, state, and city
  71. Laravel Hashids – Generate short unique hashes
  72. Laravel Repository – Repository pattern for Laravel
  73. Laravel UUID – UUID generation for models
  74. Spatie Laravel Medialibrary Pro – Enhanced media management
  75. Laravel Queue Monitor – Monitor Laravel job queues
  76. Laravel User Activity – Monitor user activity
  77. Laravel DB Snapshots – Create database snapshots
  78. Laravel Twilio – Twilio integration
  79. Laravel Roles – Role-based permission handling
  80. Laravel Translatable – Add translations to Eloquent models
  81. Laravel Teamwork – Manage teams in multi-tenant apps
  82. Laravel Full Text Search – Add full-text search to Laravel models
  83. Laravel File Manager – File and media management
  84. Laravel User Timezones – Automatically detect user time zones
  85. Laravel ChartsJS – Render charts with ChartsJS
  86. Laravel Stripe – Stripe API integration
  87. Laravel PDF Generator – PDF generation
  88. Laravel Elasticsearch – Elasticsearch integration
  89. Laravel Simple Qrcode – Generate QR codes
  90. Laravel Timezone – Manage timezones and conversions
  91. Laravel Collective API – API management for Laravel
  92. Laravel Rest API Boilerplate – REST API starter kit
  93. Laravel Multi Auth – Multi-authentication functionality
  94. Laravel Voyager – Admin panel for Laravel
  95. Laravel Voyager Database – Database manager for Voyager
  96. Laravel Categories – Handle categories for models
  97. Laravel Multitenancy – Multi-tenancy implementation
  98. Laravel Access Control – Advanced access control for users
  99. Laravel Menus – Menu management
  100. Laravel Translatable Routes – Multilingual route handling

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top