{"id":22160629,"url":"https://github.com/jpkleemans/attribute-events","last_synced_at":"2025-05-14T21:04:22.255Z","repository":{"id":36551978,"uuid":"228425178","full_name":"jpkleemans/attribute-events","owner":"jpkleemans","description":"🔥 Fire events on attribute changes of your Eloquent model","archived":false,"fork":false,"pushed_at":"2024-11-06T16:15:27.000Z","size":391,"stargazers_count":323,"open_issues_count":4,"forks_count":21,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-15T05:16:43.371Z","etag":null,"topics":["accessors","attributes","ddd","domain-driven-design","domain-events","domain-model","eloquent","event-driven-architecture","event-storming","events","laravel","model-events"],"latest_commit_sha":null,"homepage":"https://attribute.events","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/jpkleemans.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","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},"funding":{"github":"jpkleemans"}},"created_at":"2019-12-16T16:10:48.000Z","updated_at":"2025-02-19T18:48:20.000Z","dependencies_parsed_at":"2023-12-19T17:25:25.895Z","dependency_job_id":"0c0e346d-5c8f-4b4a-8d3d-96c009b5e0cf","html_url":"https://github.com/jpkleemans/attribute-events","commit_stats":{"total_commits":178,"total_committers":6,"mean_commits":"29.666666666666668","dds":0.0337078651685393,"last_synced_commit":"f28a0588eb0a10955f14d75db81314c5954761c8"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpkleemans%2Fattribute-events","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpkleemans%2Fattribute-events/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpkleemans%2Fattribute-events/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpkleemans%2Fattribute-events/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpkleemans","download_url":"https://codeload.github.com/jpkleemans/attribute-events/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254227605,"owners_count":22035668,"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":["accessors","attributes","ddd","domain-driven-design","domain-events","domain-model","eloquent","event-driven-architecture","event-storming","events","laravel","model-events"],"created_at":"2024-12-02T04:09:05.548Z","updated_at":"2025-05-14T21:04:22.232Z","avatar_url":"https://github.com/jpkleemans.png","language":"PHP","funding_links":["https://github.com/sponsors/jpkleemans"],"categories":["Packages"],"sub_categories":["Database/Eloquent/Models"],"readme":"![Laravel Attribute Events](/../gh-pages/attribute-events.svg#gh-light-mode-only)\n![Laravel Attribute Events](/../gh-pages/attribute-events-dark.svg#gh-dark-mode-only)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://app.travis-ci.com/jpkleemans/attribute-events\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/travis/com/jpkleemans/attribute-events?label=tests\u0026style=flat-square\" alt=\"Build Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/jpkleemans/attribute-events/commits\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/github/last-commit/jpkleemans/attribute-events?label=updated\u0026style=flat-square\" alt=\"Last Updated\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/jpkleemans/attribute-events\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/jpkleemans/attribute-events?label=stable\u0026style=flat-square\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/jpkleemans/attribute-events\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/jpkleemans/attribute-events?style=flat-square\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n```php\nclass Order extends Model\n{\n    protected $dispatchesEvents = [\n        'status:shipped' =\u003e OrderShipped::class,\n        'note:*' =\u003e OrderNoteChanged::class,\n    ];\n}\n```\n\nEloquent models fire several handy events throughout their lifecycle, like `created` and `deleted`. However, there are usually many more business meaningful events that happen during a model's life. With this library you can capture those, by mapping attribute changes to your own event classes.\n\n## Installation\n```bash\ncomposer require jpkleemans/attribute-events\n```\n\n## How to use it\nUse the `Kleemans\\AttributeEvents` trait in your model and add the attributes to the `$dispatchesEvents` property:\n\n```php\nclass Order extends Model\n{\n    use AttributeEvents;\n\n    protected $dispatchesEvents = [\n        'created'         =\u003e OrderPlaced::class,\n        'status:canceled' =\u003e OrderCanceled::class,\n        'note:*'          =\u003e OrderNoteChanged::class,\n    ];\n}\n```\n\nThe attribute events will be dispatched after the updated model is saved. Each event receives the instance of the model through its constructor.\n\n\u003e For more info on model events and the `$dispatchesEvents` property, visit the \u003ca href=\"https://laravel.com/docs/eloquent#events\" target=\"_blank\"\u003eLaravel Docs\u003c/a\u003e\n\n## Listening\nNow you can subscribe to the events via the `EventServiceProvider` `$listen` array, or manually with Closure based listeners:\n\n```php\nEvent::listen(function (OrderCanceled $event) {\n    // Restock inventory\n});\n```\n\nOr push realtime updates to your users, using Laravel's \u003ca href=\"https://laravel.com/docs/broadcasting\" target=\"_blank\"\u003ebroadcasting\u003c/a\u003e feature:\n\n```js\nEcho.channel('orders')\n    .listen('OrderShipped', (event) =\u003e {\n        // Display a notification\n    })\n```\n\n## JSON attributes\nFor attributes stored as JSON, you can use the `-\u003e` operator:\n\n```php\nprotected $dispatchesEvents = [\n    'payment-\u003estatus:completed' =\u003e PaymentCompleted::class,\n];\n```\n\n## Accessors\nFor more complex state changes, you can use attributes defined by an \u003ca href=\"https://laravel.com/docs/eloquent-mutators#defining-an-accessor\" target=\"_blank\"\u003eaccessor\u003c/a\u003e:\n\n```php\nclass Product extends Model\n{\n    protected $dispatchesEvents = [\n        'low_stock:true' =\u003e ProductReachedLowStock::class,\n    ];\n\n    public function getLowStockAttribute(): bool\n    {\n        return $this-\u003estock \u003c= 3;\n    }\n}\n```\n\n\u003e You can also use the [new way of defining accessors](https://laravel.com/docs/9.x/releases#eloquent-accessors-and-mutators) introduced in Laravel 9.\n\n## Learn more\n- [“Decouple your Laravel code using Attribute Events”](https://jpkleemans.medium.com/decouple-your-laravel-code-using-attribute-events-de8f2528f46a) by Jan-Paul Kleemans\n- [Laravel Docs on Model Events](https://laravel.com/docs/eloquent#events)\n\n## Sponsors\n\n\u003ca href=\"https://www.nexxtmove.nl/\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/jpkleemans/attribute-events/gh-pages/nexxtmove-logo.svg\" alt=\"Nexxtmove Logo\" width=\"200\"\u003e\n\u003c/a\u003e\n\nThanks to \u003ca href=\"https://www.nexxtmove.nl/\" target=\"_blank\"\u003eNexxtmove\u003c/a\u003e for sponsoring the development of this project.  \nYour logo or name here? [Sponsor this project](https://github.com/sponsors/jpkleemans).\n\n## License\n\nCode released under the [MIT License](https://github.com/jpkleemans/attribute-events/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpkleemans%2Fattribute-events","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpkleemans%2Fattribute-events","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpkleemans%2Fattribute-events/lists"}