{"id":21561673,"url":"https://github.com/testmonitor/laravel-accountable","last_synced_at":"2025-04-10T12:06:32.818Z","repository":{"id":20253361,"uuid":"89096388","full_name":"testmonitor/laravel-accountable","owner":"testmonitor","description":"Tracks the user responsible for creating, modifying, or deleting an Eloquent model","archived":false,"fork":false,"pushed_at":"2024-11-27T11:02:41.000Z","size":103,"stargazers_count":8,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T15:17:27.445Z","etag":null,"topics":["created","deleted","eloquent","laravel","updated","user"],"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/testmonitor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-22T20:12:45.000Z","updated_at":"2024-11-27T11:01:38.000Z","dependencies_parsed_at":"2024-11-18T09:16:54.246Z","dependency_job_id":null,"html_url":"https://github.com/testmonitor/laravel-accountable","commit_stats":{"total_commits":79,"total_committers":4,"mean_commits":19.75,"dds":"0.25316455696202533","last_synced_commit":"badfd6763a5932e6af1ebb0d4bc24330e8be5617"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Flaravel-accountable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Flaravel-accountable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Flaravel-accountable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Flaravel-accountable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testmonitor","download_url":"https://codeload.github.com/testmonitor/laravel-accountable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045267,"owners_count":21038555,"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","deleted","eloquent","laravel","updated","user"],"created_at":"2024-11-24T09:27:34.193Z","updated_at":"2025-04-10T12:06:32.780Z","avatar_url":"https://github.com/testmonitor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Accountable Eloquent models\n\n[![Latest Stable Version](https://poser.pugx.org/testmonitor/laravel-accountable/v/stable)](https://packagist.org/packages/testmonitor/laravel-accountable)\n[![CircleCI](https://img.shields.io/circleci/project/github/testmonitor/laravel-accountable.svg)](https://circleci.com/gh/testmonitor/laravel-accountable)\n[![StyleCI](https://styleci.io/repos/89096388/shield)](https://styleci.io/repos/89096388)\n[![codecov](https://codecov.io/gh/testmonitor/laravel-accountable/graph/badge.svg?token=Y1ZNUEPF8U)](https://codecov.io/gh/testmonitor/laravel-accountable)\n[![License](https://poser.pugx.org/testmonitor/laravel-accountable/license)](https://packagist.org/packages/laravel-accountable)\n\nThis package provides a trait that tracks the user responsible for creating, modifying, or\ndeleting an Eloquent model.\n\nAccountable will observe any activity on your models and it sets the **created_by_user_id**, **updated_by_user_id**, and **deleted_by_user_id**\naccordingly using the currently authenticated user.\n\nIt also provides you with some useful scope query functions, allowing you to fetch models that were either created, modified, or deleted\nby a specific user.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n    * [Using the Migration helper](#using-the-migration-helper)\n    * [Using the Trait](#using-the-trait)\n- [Examples](#examples)\n- [Tests](#tests)\n- [Changelog](#changelog)\n- [Contributing](#contributing)\n- [Credits](#credits)\n- [License](#license)\n\n## Installation\n\nThis package can be installed through Composer:\n\n```sh\n$ composer require testmonitor/laravel-accountable\n```\n\nThe package will automatically register itself.\n\nOptionally, publish the configuration file:\n\n```sh\n$ php artisan vendor:publish --provider=\"TestMonitor\\Accountable\\AccountableServiceProvider\" --tag=\"config\"\n```\n\nThe configuration file allows you to set the preferred authentication driver, the database\ncolumn names, and anonymous user. The latter can be used to deal with records created/updated by\nunauthenticated users.\n\nWhen left untouched, Accountable will use the default authentication driver and\nthe default column names (*created_by_user_id*, *updated_by_user_id*, and *deleted_by_user_id*).\n\n## Usage\n\nIn order to add Accountable to your Laravel application, you'll need to:\u003cbr /\u003e\n\n1. Add the required columns to your migration file(s).\n2. Use the trait ```TestMonitor\\Accountable\\Traits\\Accountable``` on your model(s).\n\n*Please note that due to the nature of Laravel event system, mass updates\nwill not be handled by Accountable.*\n\n### Using the Migration helper\n\nThe migration helper simplifies the process of adding columns to your migration:\n\n```php\nuse TestMonitor\\Accountable\\Accountable;\n\nclass CreateProjectsTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('projects', function (Blueprint $table) {\n            $table-\u003eincrements('id');\n            $table-\u003estring('name');\n\n            $table-\u003etimestamps();\n            $table-\u003esoftDeletes();\n\n            // This will add the required columns\n            Accountable::columns($table);\n        });\n    }\n}\n```\n\nTip: if you do not use soft-deletes on your model, use `Accountable::columns($table, false)` to prevent\nthe helper from adding a *deleted_by_user_id* column.\n\n### Using the Trait\n\nAdd the Accountable trait on the models you want to track:\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\SoftDeletes;\nuse TestMonitor\\Accountable\\Traits\\Accountable;\n\nclass Project extends Model\n{\n    use Accountable, SoftDeletes;\n\n    protected $table = 'projects';\n}\n```\n\n## Examples\n\nSet up your model and make sure you are authenticated.\n\n### Basics\n\nCreate a project and show the name of the user that created it:\n\n```php\n$project = new Project(['name' =\u003e 'Awesome project']);\n$project-\u003esave();\n\n// Show the name of user that created the project\necho $project-\u003ecreator-\u003ename;\n```\n\nGet all projects created by a specific user:\n\n```php\n$user = User::findOrFail(42);\n\n// Get all projects created by user with id 42\nProject::onlyCreatedBy($user)-\u003eget();\n```\n\n### Properties\n\nYou can use the following properties and methods to reveal the user responsible\nfor creating, updating or deleting the record.\n\n```php\n// Get the user that created the model\n$model-\u003ecreated_by_user_id;\n$model-\u003ecreator-\u003ename;\n\n// Get the user that last updated the model\n$model-\u003eupdated_by_user_id;\n$model-\u003eeditor-\u003ename;\n\n// Get the user that last deleted the model\n$model-\u003edeleted_by_user_id;\n$model-\u003edeleter-\u003ename;\n```\n\n### Scope Queries\n\nThe following scope queries are at your disposal:\n\n```php\n// Retrieve the models either created, updated,\n// or deleted by $user.\nModel::onlyCreatedBy($user)-\u003eget();\nModel::onlyUpdatedBy($user)-\u003eget();\nModel::onlyDeletedBy($user)-\u003eget();\n\n// And one extra: get all models that were created\n// by the currently authenticated user.\nModel::mine()-\u003eget();\n```\n\n### Disable Logging\n\nIn some cases, you don't want to automatically save the user along\nwith the model (for example: when seeding test data). You can disable\naccountable by using the `disableUserLogging` method.\n\n```php\n$project = new Project(['name' =\u003e 'Do not track me']);\n$project-\u003edisableUserLogging()-\u003esave();\n```\n\nIf you want to re-enable accountable, simply use the `enableUserLogging`\nmethod afterwards.\n\n### Impersonation\n\nWhen authentication is not available - for example, when running jobs\nin a queue - you might want to \"impersonate\" a user. Simply override\nuser identification with the `actingAs` method:\n\n```php\naccountable()-\u003eactingAs($event-\u003ecauser);\n```\n\nYou can end the impersonation by calling the `reset` method.\n\n## Tests\n\nThe package contains integration tests. You can run them using PHPUnit.\n\n```\n$ vendor/bin/phpunit\n```\n\n## Changelog\n\nRefer to [CHANGELOG](CHANGELOG.md) for more information.\n\n## Contributing\n\nRefer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\n## Credits\n\n* **Thijs Kok** - *Lead developer* - [ThijsKok](https://github.com/thijskok)\n* **Stephan Grootveld** - *Developer* - [Stefanius](https://github.com/stefanius)\n* **Frank Keulen** - *Developer* - [FrankIsGek](https://github.com/frankisgek)\n* **Muriel Nooder** - *Developer* - [ThaNoodle](https://github.com/thanoodle)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Refer to the [License](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestmonitor%2Flaravel-accountable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestmonitor%2Flaravel-accountable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestmonitor%2Flaravel-accountable/lists"}