{"id":13620376,"url":"https://github.com/spatie/laravel-model-flags","last_synced_at":"2025-05-14T13:07:45.196Z","repository":{"id":61768983,"uuid":"555001574","full_name":"spatie/laravel-model-flags","owner":"spatie","description":"Add flags to Eloquent models","archived":false,"fork":false,"pushed_at":"2025-05-12T21:32:48.000Z","size":548,"stargazers_count":416,"open_issues_count":1,"forks_count":21,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-12T22:34:07.047Z","etag":null,"topics":["eloquent","flags","laravel","model"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"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":"2022-10-20T19:24:28.000Z","updated_at":"2025-05-12T21:32:45.000Z","dependencies_parsed_at":"2024-03-28T15:30:57.680Z","dependency_job_id":"4f763cc7-dcc1-4c17-8cc7-8e979afa33da","html_url":"https://github.com/spatie/laravel-model-flags","commit_stats":{"total_commits":76,"total_committers":10,"mean_commits":7.6,"dds":0.3421052631578947,"last_synced_commit":"736dcd576753cac257297e47a1f84c068fbb5a3b"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-flags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-flags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-flags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-flags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-model-flags/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253837368,"owners_count":21971980,"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":["eloquent","flags","laravel","model"],"created_at":"2024-08-01T21:00:55.117Z","updated_at":"2025-05-14T13:07:40.162Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"/art/socialcard.png\" alt=\"Social Card of Laravel Model Flags\"\u003e\u003c/p\u003e\n\n# Add flags to Eloquent models\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-model-flags.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-model-flags)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-model-flags.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-model-flags)\n\nThis package offers a trait that allows you to add flags to an Eloquent model. These can be used to quickly save the state of a process, update, migration, etc... to a model, without having to add an additional column using migrations.\n\n```php\n$user-\u003ehasFlag('receivedMail'); // returns false\n\n$user-\u003eflag('receivedMail'); // flag the user as having received the mail\n\n$user-\u003ehasFlag('receivedMail'); // returns true\n```\n\nIt also provides scopes to quickly query all models with a certain flag.\n\n```php\nUser::flagged('myFlag')-\u003eget(); // returns all models with the given flag\nUser::notFlagged('myFlag')-\u003eget(); // returns all models without the given flag\n```\n\nThough there are other usages, the primary use case of this package is to easily build idempotent (aka restartable) pieces of code. For example, when writing an Artisan command that sends a mail to each user. Using flags, you can make sure that when the command is cancelled (or fails) half-way through, in the second invocation, a mail will only be sent to users that haven't received one yet.\n\n```php\n// in an Artisan command\n\nUser::notFlagged('wasSentPromotionMail')\n    -\u003eeach(function(User $user) {\n        Mail::to($user-\u003eemail)-\u003esend(new PromotionMail())\n\n        $user-\u003eflag('wasSentPromotionMail');\n    });\n});\n```\n\nNo matter how many times you would execute this command, users would only get the mail once.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-model-flags.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-model-flags)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via Composer:\n\n```bash\ncomposer require spatie/laravel-model-flags\n```\n\nBehind the scenes, the flags and the relation to a model will be stored in the `flags` table.\n\nTo create that `flags` table, you must publish and run the migrations once with:\n\n```bash\nphp artisan vendor:publish --tag=\"model-flags-migrations\"\nphp artisan migrate\n```\n\nOptionally, you can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"model-flags-config\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n    /*\n     * The model used as the flag model.\n     */\n    'flag_model' =\u003e Spatie\\ModelFlags\\Models\\Flag::class,\n];\n```\n\n## Usage\n\nTo add flaggable behaviour to a model, simply make it use the `Spatie\\ModelFlags\\Models\\Concerns\\HasFlags` trait\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Spatie\\ModelFlags\\Models\\Concerns\\HasFlags;\n\nclass YourModel extends Model\n{\n    use HasFlags;\n}\n```\n\nThese functions will become available.\n\n```php\n// add a flag\n$model-\u003eflag('myFlag');\n\n// returns true if the model has a flag with the given name\n$model-\u003ehasFlag('myFlag');\n\n// remove a flag\n$model-\u003eunflag('myFlag');\n\n // returns an array with the name of all flags on the model\n$model-\u003eflagNames();\n\n// use the `flags` relation to delete all flags on a model\n$user-\u003eflags()-\u003edelete();\n\n// use the `flags` relation to delete a particular flag on a model\n$user-\u003eflags()-\u003ewhere('name', 'myFlag')-\u003edelete();\n```\n\nA flag can only exist once for a model. When flagging a model with the same flag again, the `updated_at` attribute of the flag will be updated.\n\n```php\n$model-\u003eflag('myFlag');\n\n// after a while\n$model-\u003eflag('myFlag'); // update_at will be updated\n```\n\nYou can get the date of the last time a flag was used on a model.\n\n```php\n$model-\u003elastFlaggedAt(); // returns the update time of the lastly updated flag\n$model-\u003elastFlaggedAt('myFlag') // returns the updated_at of the `myFlag` flag on the model\n$model-\u003elastFlaggedAt('doesNotExist') // returns null if there is no flag with the given name\n```\n\nYou'll also get these scopes:\n\n```php\n// query all models that have a flag with the given name\nYourModel::flagged('myFlag');\n\n// query all models that have do not have a flag with the given name\nYourModel::notFlagged('myFlag');\n```\n\nTo remove a flag from all models in one go, you can delete the flag using the `Spatie\\ModelFlags\\Models\\Flag` model.\n\n```php\nuse Spatie\\ModelFlags\\Models\\Flag;\n\n// remove myFlag from all models\nFlag::where('name', 'myFlag')-\u003edelete();\n```\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](https://github.com/spatie/.github/blob/main/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- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nAnd a special thanks to [Caneco](https://twitter.com/caneco) for the logo ✨\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%2Fspatie%2Flaravel-model-flags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-model-flags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-model-flags/lists"}