{"id":13828281,"url":"https://github.com/orisintel/laravel-model-auditlog","last_synced_at":"2025-06-18T18:05:48.731Z","repository":{"id":54528865,"uuid":"179685600","full_name":"orisintel/laravel-model-auditlog","owner":"orisintel","description":"Tracks changes made to models and logs their column values to individual tables","archived":false,"fork":false,"pushed_at":"2021-02-12T19:35:26.000Z","size":73,"stargazers_count":24,"open_issues_count":2,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-25T07:17:57.827Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orisintel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-05T13:22:40.000Z","updated_at":"2024-12-03T13:43:28.000Z","dependencies_parsed_at":"2022-08-13T18:50:17.355Z","dependency_job_id":null,"html_url":"https://github.com/orisintel/laravel-model-auditlog","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/orisintel/laravel-model-auditlog","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisintel%2Flaravel-model-auditlog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisintel%2Flaravel-model-auditlog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisintel%2Flaravel-model-auditlog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisintel%2Flaravel-model-auditlog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orisintel","download_url":"https://codeload.github.com/orisintel/laravel-model-auditlog/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orisintel%2Flaravel-model-auditlog/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260606472,"owners_count":23035350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T09:02:39.633Z","updated_at":"2025-06-18T18:05:43.710Z","avatar_url":"https://github.com/orisintel.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Model Auditlog\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/orisintel/laravel-model-auditlog.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-model-auditlog)\n[![Build Status](https://img.shields.io/github/workflow/status/orisintel/laravel-model-auditlog/tests?style=flat-square)](https://github.com/orisintel/laravel-model-auditlog/actions?query=workflow%3Atests)\n[![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-model-auditlog.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-model-auditlog)\n\nWhen modifying a model record, it is nice to have a log of the changes made and who made those changes. There are many packages around this already, but this one is different in that it logs those changes to individual tables for performance and supports real foreign keys.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require orisintel/laravel-model-auditlog\n```\n\n## Configuration\n\n``` php\nphp artisan vendor:publish --provider=\"\\OrisIntel\\AuditLog\\AuditLogServiceProvider\"\n```\n\nRunning the above command will publish the config file.\n\n## Usage\n\nAfter adding the proper fields to your table, add the trait to your model.\n\n``` php\n// User model\nclass User extends Model\n{\n    use \\OrisIntel\\AuditLog\\Traits\\AuditLoggable;\n\n```\n\nTo generate an auditlog model / migration for your models, use the following command:\n\n```sh\nphp artisan make:model-auditlog \"\\App\\User\"\n```\n\nReplace `\\App\\User` with your own model name. Model / table options can be tweaked in the config file.\n\nIf you need to ignore specific fields on your model, extend the `getAuditLogIgnoredFields()` method and return an array of fields.\n\n```php\npublic function getAuditLogIgnoredFields() : array\n{\n    return ['posted_at'];\n}\n```\n\nUsing that functionality, you can add more custom logic around what should be logged. An example might be to not log the title changes of a post if the post has not been published yet.\n```php\npublic function getAuditLogIgnoredFields() : array\n{\n    if ($this-\u003epostHasBeenPublished()) {\n        return ['title'];\n    }\n\n    return [];\n}\n```\n\n### Working with Pivot Tables\n\nAudit log can also support changes on pivot models as well.\n\nIn this example we have a `posts` and `tags` table with a `post_tags` pivot table containing a `post_id` and `tag_id`.\n\nModify the audit log migration replacing the `subject_id` column to use the two pivot columns. \n```php\nSchema::create('post_tag_auditlog', function (Blueprint $table) {\n    $table-\u003ebigIncrements('id');\n    $table-\u003eunsignedInteger('post_id')-\u003eindex();\n    $table-\u003eunsignedInteger('tag_id')-\u003eindex();\n    $table-\u003eunsignedTinyInteger('event_type')-\u003eindex();\n    $table-\u003eunsignedInteger('user_id')-\u003enullable()-\u003eindex();\n    $table-\u003estring('field_name')-\u003eindex();\n    $table-\u003etext('field_value_old')-\u003enullable();\n    $table-\u003etext('field_value_new')-\u003enullable();\n    $table-\u003etimestamp('occurred_at')-\u003eindex()-\u003edefault('CURRENT_TIMESTAMP');\n});\n```\n\nCreate a model for the pivot table that extends Laravel's Pivot class. This class must use the AuditLoggablePivot trait and have a defined `$audit_loggable_keys` variable, which is used to map the pivot to the audit log table.\n \n```php\nclass PostTag extends Pivot\n{\n    use AuditLoggablePivot;\n\n    /**\n     * The array keys are the composite key in the audit log\n     * table while the pivot table columns are the values.\n     *\n     * @var array\n     */\n    protected $audit_loggable_keys = [\n        'post_id' =\u003e 'post_id',\n        'tag_id'  =\u003e 'tag_id',\n    ];\n}\n```\nSide note: if a column shares the same name in the pivot and a column already in the audit log table (ex: `user_id`), change the name of the column in the audit log table (ex: `audit_user_id`) and define the relationship as `'audit_user_id' =\u003e 'user_id'`.\n\nThe two models that are joined by the pivot will need to be updated so that events fire on the pivot model. Currently Laravel doesn't support pivot events so a third party package is required.\n```php\ncomposer require fico7489/laravel-pivot\n```\n\nHave both models use the PivotEventTrait\n```php\nuse Fico7489\\Laravel\\Pivot\\Traits\\PivotEventTrait;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model\n{\n    use PivotEventTrait;\n```\n\nModify the belongsToMany join on both related models to include the using function along with the pivot model.\nIn the Post model:\n```php\npublic function tags()\n{\n    return $this-\u003ebelongsToMany(Tag::class)\n        -\u003eusing(PostTag::class);\n}\n```\nIn the Tag model:\n```php\npublic function posts()\n{\n    return $this-\u003ebelongsToMany(Post::class)\n        -\u003eusing(PostTag::class);\n}\n```\n\nWhen a pivot record is deleted through `detach` or `sync`, an audit log record for each of the keys (ex: `post_id` and `tag_id`) will added to the audit log table. The `field_value_old` will be the id of the record and the `field_value_new` will be null. The records will have an event type of `PIVOT_DELETED` (id: 6). \n\nIf you need to pull the audit logs through the `auditLogs` relationship (ex: $post_tag-\u003eauditLogs()-\u003eget()), support for composite keys is required.\n```php\ncomposer require awobaz/compoships\n```\nThen use the trait on the pivot audit log model:\n```php\nuse Awobaz\\Compoships\\Compoships;\nuse OrisIntel\\AuditLog\\Models\\BaseModel;\n\nclass PostTagAuditLog extends BaseModel\n{\n    use Compoships;\n```\n\nFor a working example of pivots with the audit log, see `laravel-model-auditlog/tests/Fakes`, which contains working migrations and models.\n\nNote:\nBoth models must use the AuditLoggable trait (ex: Post and Tag) so that `$post-\u003etags()-\u003esync([...])` will work.\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Using Docker\nAll assets are set up under the docker-compose.yml file. The first time you run the docker image you must build it with\nthe following command:\n```bash\ndocker-compose build\n```\n\nThen you can bring it up in the background using:\n```bash\ndocker-compose up -d\n```\n\nAnd the image is aliased so you can access its command line via:\n```bash\ndocker exec -it processes-stamp-app /bin/bash\n```\n\nFrom there you can run the tests within an isolated environment\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email [security@orisintel.com](mailto:security@orisintel.com) instead of using the issue tracker.\n\n## Credits\n\n- [Tom Schlick](https://github.com/tomschlick)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forisintel%2Flaravel-model-auditlog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forisintel%2Flaravel-model-auditlog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forisintel%2Flaravel-model-auditlog/lists"}