Download the PHP package jeylabs/laravel-audit-log without Composer
On this page you can find all versions of the php package jeylabs/laravel-audit-log. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-audit-log
Log audit inside your Laravel app
The jeylabs/laravel-audit-log
package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. All activity will be stored in the audit_logs
table.
You can retrieve all activity using the Jeylabs\Auditlog\Models\AuditLog
model.
Here's a more advanced example:
Calling $auditLog->changes
will return this array:
Installation
You can install the package via composer:
Next, you must install the service provider:
You can publish the migration with:
Note: The default migration assumes you are using integers for your model IDs. If you are using UUIDs, or some other format, adjust the format of the subject_id and causer_id fields in the published migration before continuing.
After the migration has been published you can create the audit-logs
table by running the migrations:
You can optionally publish the config file with:
This is the contents of the published config file:
Logging model events
A neat feature of this package is that it can automatically log events such as when a model is created, updated and deleted. To make this work all you need to do is let your model use the Jeylabs\AuditLog\Traits\LogsAudit
-trait.
As a bonus the package will also log the changed attributes for all these events when setting $logAttributes
property on the model.
Here's an example:
Let's see what gets logged when creating an instance of that model.
Now let's update some that $newsItem
.
Calling $auditLog->changes
will return this array:
Now, what happens when you call delete?
Customizing the events being logged
By default the package will log the created
, updated
, deleted
events. You can modify this behaviour by setting the $recordEvents
property on a model.
Customizing the description
By default the package will log created
, updated
, deleted
in the description of the activity. You can modify this text by overriding the getDescriptionForEvent
function.
Let's see what happens now:
Ignoring changes to certain attributes
If your model contains attributes whose change don't need to trigger an activity being logged you can use $ignoreChangedAttributes
Changing text
will not trigger an audit being logged.
By default the updated_at
attribute is not ignored and will trigger an activity being logged. You can simply add the updated_at
attribute to the $ignoreChangedAttributes
array to override this behaviour.
Logging only the changed attributes
If you do not want to log every attribute in your $logAttributes
variable, but only those that has actually changed after the update, you can use $logOnlyDirty
Changing only name
means only the name
attribute will be logged in the activity, and text
will be left out.
Using the CausesAudit trait
The package ships with a CausesAudit
trait which can be added to any model that you use as a causer. It provides an auditLog
relationship which returns all activities that are caused by the model.
If you include it in the User
model you can simply retrieve all the current users activities like this:
All versions of laravel-audit-log with dependencies
illuminate/config Version ^5.0|^6.0
illuminate/database Version ^5.0|^6.0
illuminate/support Version ^5.0|^6.0
spatie/string Version ^2.1