{"id":20025443,"url":"https://github.com/alexstewartja/laravel-attribute-observer","last_synced_at":"2026-02-20T01:10:51.262Z","repository":{"id":37013604,"uuid":"411897263","full_name":"alexstewartja/laravel-attribute-observer","owner":"alexstewartja","description":"Observe (and react to) attribute changes made on Eloquent models.","archived":false,"fork":false,"pushed_at":"2025-03-14T16:54:10.000Z","size":46,"stargazers_count":73,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2026-02-14T17:36:21.746Z","etag":null,"topics":["attribute","eloquent","events","laravel","observer"],"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/alexstewartja.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"alexstewartja","buy_me_a_coffee":"alexstewartja"}},"created_at":"2021-09-30T02:31:29.000Z","updated_at":"2025-09-22T17:57:35.000Z","dependencies_parsed_at":"2025-04-09T12:00:41.651Z","dependency_job_id":"8e435e02-1125-49ea-b62e-e76b1105d48c","html_url":"https://github.com/alexstewartja/laravel-attribute-observer","commit_stats":{"total_commits":38,"total_committers":7,"mean_commits":5.428571428571429,"dds":"0.39473684210526316","last_synced_commit":"22d436bb66d50423045b78b909513aa3b3bb5ee0"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/alexstewartja/laravel-attribute-observer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexstewartja%2Flaravel-attribute-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexstewartja%2Flaravel-attribute-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexstewartja%2Flaravel-attribute-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexstewartja%2Flaravel-attribute-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexstewartja","download_url":"https://codeload.github.com/alexstewartja/laravel-attribute-observer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexstewartja%2Flaravel-attribute-observer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29638125,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["attribute","eloquent","events","laravel","observer"],"created_at":"2024-11-13T08:55:35.304Z","updated_at":"2026-02-20T01:10:51.238Z","avatar_url":"https://github.com/alexstewartja.png","language":"PHP","funding_links":["https://github.com/sponsors/alexstewartja","https://buymeacoffee.com/alexstewartja"],"categories":[],"sub_categories":[],"readme":"# Laravel Attribute Observer\n\n![Laravel Attribute Observer - Social Image](https://banners.beyondco.de/Laravel%20Attribute%20Observer.png?theme=light\u0026packageManager=composer+require\u0026packageName=alexstewartja%2Flaravel-attribute-observer\u0026pattern=floatingCogs\u0026style=style_2\u0026description=Observe+%28and+react+to%29+attribute+changes+made+on+Eloquent+models.\u0026md=1\u0026showWatermark=0\u0026fontSize=125px\u0026images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg)\n\n[![Latest Stable Version](http://poser.pugx.org/alexstewartja/laravel-attribute-observer/v)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)\n[![Total Downloads](http://poser.pugx.org/alexstewartja/laravel-attribute-observer/downloads)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)\n[![License](http://poser.pugx.org/alexstewartja/laravel-attribute-observer/license)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)\n\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/alexstewartja/laravel-attribute-observer/run-tests.yml?label=tests)](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/alexstewartja/laravel-attribute-observer/php-cs-fixer.yml?label=code%20style)](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n\n[![PHP Versions Supported](http://poser.pugx.org/alexstewartja/laravel-attribute-observer/require/php)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)\n[![Laravel Versions Supported](https://img.shields.io/packagist/dependency-v/alexstewartja/laravel-attribute-observer/illuminate/contracts?label=laravel)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)\n\n[![Buy Me A Coffee](https://img.shields.io/badge/Buy_Me-A_Coffee-orange?logo=buy-me-a-coffee)](https://buymeacoffee.com/alexstewartja)\n\nA Laravel package which allows you to observe (and react to) attribute changes made on Eloquent models.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require alexstewartja/laravel-attribute-observer\n```\n\n## Configuration\n\nPublish the config file (`config/attribute-observer.php`) with:\n```bash\nphp artisan vendor:publish --provider=\"AlexStewartJA\\LaravelAttributeObserver\\LaravelAttributeObserverServiceProvider\"\n```\n\nThis is the default content of the published config file:\n\n```php\nreturn [\n    /*\n    |--------------------------------------------------------------------------\n    | Attribute Observers\n    |--------------------------------------------------------------------------\n    |\n    | Here you may configure all desired Models and their respective Attribute\n    | Observers. For example:\n    |\n    | 'observers' =\u003e [\n    |    \\App\\Models\\Order::class =\u003e [\n    |        \\App\\AttributeObservers\\OrderStatusObserver::class,\n    |    ],\n    | ]\n    |\n    */\n\n    'observers' =\u003e [\n        // Define your model \u0026 attribute observers here...\n    ]\n];\n```\n\nPopulate the `observers` array with your desired **Model =\u003e Attribute Observer** mappings.\n\n## Usage\n\n### Attribute Observers\n\nThe `make:laravel-attribute-observer` Artisan command is the easiest way to create a new attribute observer class:\n\n```bash\nphp artisan make:laravel-attribute-observer OrderStatusObserver --model=Order\n```\n\nThis command will place the new attribute observer in your `App/AttributeObservers` directory. If this directory does not exist,\nArtisan will create it for you. Your freshly generated attribute observer will look like the following:\n\n```php\n\u003c?php\n\nnamespace App\\AttributeObservers;\n\nuse App\\Models\\Order;\n\nclass OrderStatusObserver\n{\n    /**\n     * Handle changes to the \"id\" field of Order on \"created\" events.\n     *\n     * @param \\App\\Models\\Order $order\n     * @param mixed $newValue The current value of the field\n     * @param mixed $oldValue The previous value of the field\n     * @return void\n     */\n    public function onIdCreated(Order $order, mixed $newValue, mixed $oldValue)\n    {\n        //\n    }\n}\n```\n\nYou may then modify the code to match your business logic, for example:\n\n```php\n\u003c?php\n\nnamespace App\\AttributeObservers;\n\nuse App\\Events\\OrderStatusChanged;\nuse App\\Models\\Order;\n\nclass OrderStatusObserver\n{\n    /**\n     * Handle changes to the \"status\" field of Order on \"saved\" events.\n     *\n     * @param \\App\\Models\\Order $order\n     * @param mixed $newValue The current value of the field\n     * @param mixed $oldValue The previous value of the field\n     * @return void\n     */\n    public function onStatusSaved(Order $order, mixed $newValue, mixed $oldValue)\n    {\n        // Dispatch an event that sends an order update email to the customer\n        OrderStatusChanged::dispatch($order);\n    }\n}\n```\n\n\u003e _Attribute Observer methods are always supplied with the **model instance**, the **new attribute value** and the **previous attribute\nvalue**, in that order._\n\n### Events\n\nYou may observe all the typical CRUD events dispatched by Eloquent models during their lifecycles. Supported events are:\n`creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`.\n\nThe naming convention to follow when defining attribute observer methods is: **on|AttributeName|Event**\n\nSo, let's say we want to check a user's email against a global spam/vanity mail list each time they attempt to update it,\nwe would implement that logic in an attribute observer method called `onEmailUpdating`.\n\n\u003e _Note that the attribute name must be in **[PascalCase](https://techterms.com/definition/pascalcase)** and the\n\u003e event name must be **[Capitalized](https://wikipedia.org/wiki/Capitalization)**._\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Alex Stewart](https://github.com/alexstewartja)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Donations\n\nI maintain this package in my spare time.\nIf it's beneficial to you, consider buying me a coffee to keep it improving.\n\n[![Buy Me A Coffee](https://img.shields.io/badge/Buy_Me-A_Coffee-orange?logo=buy-me-a-coffee)](https://buymeacoffee.com/alexstewartja)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexstewartja%2Flaravel-attribute-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexstewartja%2Flaravel-attribute-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexstewartja%2Flaravel-attribute-observer/lists"}