misakstvanu / laravel-model-log
A Laravel package for logging model changes
v1.0.5
2026-04-06 21:48 UTC
Requires
- php: >=8.1
- laravel/framework: >=10.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2026-04-06 21:50:57 UTC
README
A Laravel package that automatically logs all changes to Eloquent models for audit trails.
Installation
Install the package via Composer:
composer require misakstvanu/laravel-model-log
Publish the migration and config files:
php artisan vendor:publish --provider="Misakstvanu\ModelLog\ModelLogServiceProvider" --tag=model-log-migrations php artisan vendor:publish --provider="Misakstvanu\ModelLog\ModelLogServiceProvider" --tag=model-log-config
Run the migration:
php artisan migrate
Usage
Add the Loggable trait to any model you want to log:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Misakstvanu\ModelLog\Traits\Loggable; class User extends Model { use Loggable; // ... }
That's it! All changes to the model will be automatically logged to the model_logs table.
Logged Operations
The package logs the following operations:
create: When a new record is createdupdate: When an existing record is updateddelete: When a record is soft deleted (if the model uses soft deletes)soft_delete: When a record is soft deletedrestore: When a soft deleted record is restoredforce_delete: When a record is permanently deleted
Configuration
You can customize the behavior by editing config/model-log.php:
models.include: Array of models to log (if empty, all models with the trait are logged)models.exclude: Array of models to exclude from loggingexcluded_attributes: Attributes to exclude from logging (e.g., passwords)track_users: Whether to track the user who made the changenormalize_datetime_to_db_format: Normalize date/datetime values to DB format before logging (default:false), useful to avoid false-positive changes when equivalent datetime values use different formatsuser_model: Custom user model class
Database Schema
The model_logs table contains:
model_class: The class name of the modelmodel_id: The primary key of the modeloperation: The type of operationold_values: JSON of the old attribute values (for updates/deletes)new_values: JSON of the new attribute values (for creates/updates)user_id: The ID of the user who made the change (nullable)created_at: Timestamp of the change
Testing
Run the package tests with:
vendor/bin/phpunit -c phpunit.xml.dist
License
This package is open-sourced software licensed under the MIT license.