{"id":13828252,"url":"https://github.com/sebastiaanluca/laravel-boolean-dates","last_synced_at":"2026-02-15T02:35:03.090Z","repository":{"id":42020181,"uuid":"142449479","full_name":"sebastiaanluca/laravel-boolean-dates","owner":"sebastiaanluca","description":"Automatically convert Eloquent model boolean attributes to dates (and back).","archived":false,"fork":false,"pushed_at":"2025-11-28T15:51:52.000Z","size":124,"stargazers_count":40,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-30T21:35:01.892Z","etag":null,"topics":["attributes","boolean","dates","eloquent","gdpr","laravel","model","php"],"latest_commit_sha":null,"homepage":"https://sebastiaanluca.com","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/sebastiaanluca.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-07-26T14:05:46.000Z","updated_at":"2025-11-28T15:51:56.000Z","dependencies_parsed_at":"2024-07-18T02:35:50.417Z","dependency_job_id":"c117528d-7ce0-4554-9598-f54966b2dcc4","html_url":"https://github.com/sebastiaanluca/laravel-boolean-dates","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"purl":"pkg:github/sebastiaanluca/laravel-boolean-dates","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Flaravel-boolean-dates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Flaravel-boolean-dates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Flaravel-boolean-dates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Flaravel-boolean-dates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastiaanluca","download_url":"https://codeload.github.com/sebastiaanluca/laravel-boolean-dates/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Flaravel-boolean-dates/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29465768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-15T01:01:38.065Z","status":"online","status_checked_at":"2026-02-15T02:00:07.449Z","response_time":118,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["attributes","boolean","dates","eloquent","gdpr","laravel","model","php"],"created_at":"2024-08-04T09:02:38.480Z","updated_at":"2026-02-15T02:35:03.078Z","avatar_url":"https://github.com/sebastiaanluca.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Convert Eloquent boolean attributes to dates (and back)\n\n[![Latest stable release][version-badge]][link-packagist]\n[![Software license][license-badge]](LICENSE.md)\n[![Total downloads][downloads-badge]][link-packagist]\n[![Total stars][stars-badge]][link-github]\n\n[![Visit my website][website-link-badge]][link-website]\n[![View my other packages and projects][packages-link-badge]][link-packages]\n[![Follow @sebastiaanluca on Twitter][twitter-profile-badge]][link-twitter]\n[![Share this package on Twitter][twitter-share-badge]][link-twitter-share]\n\n**Automatically convert Eloquent model boolean fields to dates (and back to booleans)** so you always know _when_ something was accepted or changed.\n\nSay you've got a registration page for users where they need to accept your terms and perhaps can opt-in to certain features using checkboxes. With the GDPR privacy laws, you're somewhat required to not just keep track of the fact *if* they accepted those (or not), but also *when* they did.\n\n### Example\n\nUser registration controller:\n\n```php\n$input = request()-\u003einput();\n\n$user = User::create([\n    'has_accepted_terms' =\u003e $input['terms'],\n    'is_subscribed_to_newsletter' =\u003e $input['newsletter'],\n]);\n```\n\nAnywhere else in your code:\n\n```php\n// true or false (boolean)\n$user-\u003ehas_accepted_terms;\n\n// 2018-05-10 16:24:22 (Carbon instance)\n$user-\u003eaccepted_terms_at;\n```\n\n## Table of contents\n\n- [Requirements](#requirements)\n- [How to install](#how-to-install)\n- [How to use](#how-to-use)\n    - [Saving dates](#saving-dates)\n    - [Clearing saved values](#clearing-saved-values)\n    - [Retrieving values](#retrieving-values)\n        - [Retrieving fields as booleans](#retrieving-fields-as-booleans)\n        - [Retrieving fields as datetimes](#retrieving-fields-as-datetimes)\n    - [Array conversion](#array-conversion)\n- [License](#license)\n- [Change log](#change-log)\n- [Testing](#testing)\n- [Contributing](#contributing)\n- [Security](#security)\n- [Credits](#credits)\n- [About](#about)\n\n## Requirements\n\n- PHP 8.4 or 8.5\n- Laravel 11 or 12\n\n## How to install\n\n**Add the package** to your project using composer:\n\n```bash\ncomposer require sebastiaanluca/laravel-boolean-dates\n```\n\n**Set up your Eloquent model** by:\n\n1. Adding your datetime columns to the `$casts` property or `casts()` method\n2. Adding the boolean attributes to `$appends`\n3. Creating attribute accessors and mutators for each field\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse SebastiaanLuca\\BooleanDates\\BooleanDateAttribute;\n\nclass User extends Model\n{\n    /**\n     * The attributes that should be cast to native types.\n     *\n     * @var array\u003cstring, string\u003e\n     */\n    protected $casts = [\n        'accepted_terms_at' =\u003e 'immutable_datetime',\n        'subscribed_to_newsletter_at' =\u003e 'datetime',\n    ];\n\n    /**\n     * The accessors to append to the model's array form.\n     *\n     * @var array\u003cint, string\u003e\n     */\n    protected $appends = [\n        'has_accepted_terms',\n        'is_subscribed_to_newsletter',\n    ];\n\n    protected function hasAcceptedTerms(): Attribute\n    {\n        return BooleanDateAttribute::for('accepted_terms_at');\n    }\n\n    protected function isSubscribedToNewsletter(): Attribute\n    {\n        return BooleanDateAttribute::for('subscribed_to_newsletter_at');\n    }\n}\n```\n\nOptionally, if your database table hasn't got the datetime columns yet, create a **migration** to create a new table or alter your existing table to add the timestamp fields:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nreturn new class extends Migration {\n    public function up(): void\n    {\n        Schema::table('users', static function (Blueprint $table): void {\n            $table-\u003etimestamp('accepted_terms_at')-\u003enullable();\n            $table-\u003etimestamp('subscribed_to_newsletter_at')-\u003enullable();\n        });\n    }\n};\n\n```\n\n## How to use\n\n### Saving dates\n\nIf a boolean date field's value is true-ish, it'll be automatically converted to the current datetime. You can use anything like booleans, strings, positive integers, and so on.\n\n```php\n$user = new User;\n\n// Setting values explicitly\n$user-\u003ehas_accepted_terms = true;\n$user-\u003ehas_accepted_terms = 'yes';\n$user-\u003ehas_accepted_terms = '1';\n$user-\u003ehas_accepted_terms = 1;\n\n// Or using attribute filling\n$user-\u003efill(['is_subscribed_to_newsletter' =\u003e 'yes']);\n\n$user-\u003esave();\n```\n\nAll fields should now contain a datetime similar to `2018-05-10 16:24:22`.\n\nNote that the date stored in the database column **is immutable, i.e. it's only set once**. Any following updates will not change the stored date(time), unless you update the date column manually or if you set it to `false` and back to `true` (disabling, then enabling it).\n\nFor example:\n\n```php\n$user = new User;\n\n$user-\u003ehas_accepted_terms = true;\n$user-\u003esave();\n\n// `accepted_terms_at` column will contain `2022-03-13 13:20:00`\n\n$user-\u003ehas_accepted_terms = true;\n$user-\u003esave();\n\n// `accepted_terms_at` column will still contain the original `2022-03-13 13:20:00` date\n```\n\n### Clearing saved values\n\nOf course you can also remove the saved date and time, for instance if a user retracts their approval:\n\n```php\n$user = User::findOrFail(42);\n\n$user-\u003ehas_accepted_terms = false;\n$user-\u003ehas_accepted_terms = null;\n$user-\u003ehas_accepted_terms = '0';\n$user-\u003ehas_accepted_terms = 0;\n$user-\u003ehas_accepted_terms = '';\n// $user-\u003ehas_accepted_terms = null;\n\n$user-\u003esave();\n```\n\nFalse or false-y values are converted to `NULL`.\n\n### Retrieving values\n\n#### Retrieving fields as booleans\n\nUse a boolean field's defined _key_ to access its boolean value:\n\n```php\n$user = User::findOrFail(42);\n\n// true or false (boolean)\n$user-\u003ehas_accepted_terms;\n```\n\n#### Retrieving fields as datetimes\n\nUse a boolean field's defined _value_ to explicitly access its (Carbon) datetime value:\n\n```php\n$user = User::findOrFail(42);\n\n// 2018-05-10 16:24:22 (Carbon or CarbonImmutable instance)\n$user-\u003eaccepted_terms_at;\n\n// null\n$user-\u003eis_subscribed_to_newsletter;\n```\n\n### Array conversion\n\nWhen converting a model to an array, the boolean fields will be included if you've added them to the `$appends` array in your model.\n\n```php\n$user = User::findOrFail(42);\n\n$user-\u003etoArray();\n\n/*\n * Which will return something like:\n * \n * [\n *     'accepted_terms_at' =\u003e \\Carbon\\CarbonImmutable('2018-05-10 16:24:22'),\n *     'subscribed_to_newsletter_at' =\u003e \\Illuminate\\Support\\Carbon('2018-05-10 16:24:22'),\n *     'has_accepted_terms' =\u003e true,\n *     'is_subscribed_to_newsletter' =\u003e true,\n * ];\n */\n```\n\n## License\n\nThis package operates under the MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\ncomposer install\ncomposer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email [hello@sebastiaanluca.com][link-author-email] instead of using the issue tracker.\n\n## Credits\n\n- [Sebastiaan Luca][link-github-profile]\n- [All Contributors][link-contributors]\n\n## About\n\nMy name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel web apps. Check out my [website][link-website] for more information and my other [packages][link-packages] to kick-start your next project.\n\nHave a project that could use some guidance? Send me an e-mail at [hello@sebastiaanluca.com][link-author-email]!\n\n[version-badge]: https://img.shields.io/packagist/v/sebastiaanluca/laravel-boolean-dates.svg?label=stable\n[license-badge]: https://img.shields.io/badge/license-MIT-informational.svg\n[downloads-badge]: https://img.shields.io/packagist/dt/sebastiaanluca/laravel-boolean-dates.svg?color=brightgreen\n[stars-badge]: https://img.shields.io/github/stars/sebastiaanluca/laravel-boolean-dates.svg?color=brightgreen\n\n[website-link-badge]: https://img.shields.io/badge/link-website-lightgrey.svg\n[packages-link-badge]: https://img.shields.io/badge/link-packages-lightgrey.svg\n[twitter-profile-badge]: https://img.shields.io/twitter/follow/sebastiaanluca.svg?style=social\n[twitter-share-badge]: https://img.shields.io/twitter/url/http/shields.io.svg?style=social\n\n[link-github]: https://github.com/sebastiaanluca/laravel-boolean-dates\n[link-packagist]: https://packagist.org/packages/sebastiaanluca/laravel-boolean-dates\n[link-twitter-share]: https://twitter.com/intent/tweet?text=Easily%20convert%20Eloquent%20model%20booleans%20to%20dates%20and%20back%20with%20Laravel%20Boolean%20Dates.%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/laravel-boolean-dates\n[link-contributors]: ../../contributors\n\n[link-website]: https://sebastiaanluca.com\n[link-packages]: https://packagist.org/packages/sebastiaanluca\n[link-twitter]: https://twitter.com/sebastiaanluca\n[link-github-profile]: https://github.com/sebastiaanluca\n[link-author-email]: mailto:hello@sebastiaanluca.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanluca%2Flaravel-boolean-dates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastiaanluca%2Flaravel-boolean-dates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanluca%2Flaravel-boolean-dates/lists"}