Download the PHP package esign/laravel-database-auditing without Composer
On this page you can find all versions of the php package esign/laravel-database-auditing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download esign/laravel-database-auditing
More information about esign/laravel-database-auditing
Files in esign/laravel-database-auditing
Package laravel-database-auditing
Short Description Track database changes in Laravel using database triggers.
License MIT
Homepage https://github.com/esign/laravel-database-auditing
Informations about the package laravel-database-auditing
Track database changes in Laravel using database triggers.
This package allows you to track changes in your database using database triggers. Currently only MySQL is supported.
Note This package is designed to track database changes that occur outside of Laravel. However, for changes originating within a Laravel application, other solutions like owen-it/laravel-auditing may better suit your requirements.
Installation
You can install the package via composer:
The package will automatically register a service provider.
This package comes with a migration to store your database changes. You can publish the migration file:
Running this command will publish the following migration:
Next up, you can optionally publish the configuration file:
The config file will be published as config/database-auditing.php with the following contents:
Usage
Creating database triggers
To track database changes, use the php artisan make:audit-trigger
command. This will generate a migration file with the necessary trigger configuration:
By default, a trigger name will be automatically assigned based on the provided input. However, you can specify a different name by passing it as the first argument:
Retrieving tracked changes
After running the trigger migration, any modifications made to the associated table will be automatically monitored and stored in the audits
table.
Here is a representation of how audits are stored in the audits table based on different trigger events: |
id | event | auditable_type | auditable_id | old_data | new_data |
---|---|---|---|---|---|---|
1 | insert | post | 1 | NULL | {"title": "My Post"} | |
2 | update | post | 1 | {"title": "My Post"} | {"title": "My Updated Post"} | |
3 | delete | post | 1 | {"title": "My Post"} | NULL |
To retrieve the recorded changes in your Laravel project, you can utilize the Esign\DatabaseAuditing\Models\Audit
model provided by the package.
Here's an example:
To determine if any data changes occurred in an audit, you can utilize the hasDataChanges()
method available on the Audit
model.
Here's how you can use it:
The hasDataChanges()
method returns a boolean value indicating whether any changes were made.
If you pass a specific attribute name as an argument, it will check for changes in that particular attribute only.
To retrieve audits based on specific trigger events, you can use the event()
scope provided by the Audit
model.
Here's an example:
Tracking changes in Eloquent models
To track changes related to your Eloquent model, apply the Esign\DatabaseAuditing\Concerns\HasAudits
trait to the respective model. For instance:
This will enable an audits
relationship on the model, allowing you to access the tracked changes. For example:
Feel free to explore the Esign\DatabaseAuditing\Models\Audit
model for more functionality related to tracking and retrieving changes.
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-database-auditing with dependencies
esign/laravel-database-trigger Version ^1.5.0
illuminate/database Version ^12.0
illuminate/support Version ^12.0