PHPackages                             sebastiaanluca/laravel-boolean-dates - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sebastiaanluca/laravel-boolean-dates

ActiveLibrary[Database &amp; ORM](/categories/database)

sebastiaanluca/laravel-boolean-dates
====================================

Automatically convert Eloquent model boolean attributes to dates (and back).

11.0.0(2mo ago)40111.7k↑23.4%11MITPHPPHP ^8.4.0CI passing

Since Jul 26Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/sebastiaanluca/laravel-boolean-dates)[ Packagist](https://packagist.org/packages/sebastiaanluca/laravel-boolean-dates)[ Docs](https://github.com/sebastiaanluca/laravel-boolean-dates)[ RSS](/packages/sebastiaanluca-laravel-boolean-dates/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (31)Used By (1)

Convert Eloquent boolean attributes to dates (and back)
=======================================================

[](#convert-eloquent-boolean-attributes-to-dates-and-back)

[![Latest stable release](https://camo.githubusercontent.com/57f110c85da94629bc62c0d177d5cf5c558c9cfc50e92304e8084753cc351ffe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365626173746961616e6c7563612f6c61726176656c2d626f6f6c65616e2d64617465732e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/sebastiaanluca/laravel-boolean-dates)[![Software license](https://camo.githubusercontent.com/08662bb339bee903c7279b82f3217310d3f196fdfa63b019d294ae85e1a5d7d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d696e666f726d6174696f6e616c2e737667)](LICENSE.md)[![Total downloads](https://camo.githubusercontent.com/6fb3708993823c9d0925510783ba83f0f8124cfb3330f5eccd31256b09edfc55/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7365626173746961616e6c7563612f6c61726176656c2d626f6f6c65616e2d64617465732e7376673f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/sebastiaanluca/laravel-boolean-dates)[![Total stars](https://camo.githubusercontent.com/8a018f44f8869c3cabfac87409f0ac6bd5518eaf5cef410839b9b523a4272459/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f7365626173746961616e6c7563612f6c61726176656c2d626f6f6c65616e2d64617465732e7376673f636f6c6f723d627269676874677265656e)](https://github.com/sebastiaanluca/laravel-boolean-dates)

[![Visit my website](https://camo.githubusercontent.com/e7ba677e5ab3656d011565d194b881ee2051defb4970c19b63e8a210a53d9794/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d776562736974652d6c69676874677265792e737667)](https://sebastiaanluca.com)[![View my other packages and projects](https://camo.githubusercontent.com/a8bf33ca420b75cf9993c9b41f14d20621ff4e64f89f25afd20ade9ebba5f4bb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d7061636b616765732d6c69676874677265792e737667)](https://packagist.org/packages/sebastiaanluca)[![Follow @sebastiaanluca on Twitter](https://camo.githubusercontent.com/bde346ed8efc8b2458386b36256c64e3fe010276f449b80fe4f663c4c8bbd83e/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f7365626173746961616e6c7563612e7376673f7374796c653d736f6369616c)](https://twitter.com/sebastiaanluca)[![Share this package on Twitter](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](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)

**Automatically convert Eloquent model boolean fields to dates (and back to booleans)** so you always know *when* something was accepted or changed.

Say 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.

### Example

[](#example)

User registration controller:

```
$input = request()->input();

$user = User::create([
    'has_accepted_terms' => $input['terms'],
    'is_subscribed_to_newsletter' => $input['newsletter'],
]);
```

Anywhere else in your code:

```
// true or false (boolean)
$user->has_accepted_terms;

// 2018-05-10 16:24:22 (Carbon instance)
$user->accepted_terms_at;
```

Table of contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [How to install](#how-to-install)
- [How to use](#how-to-use)
    - [Saving dates](#saving-dates)
    - [Clearing saved values](#clearing-saved-values)
    - [Retrieving values](#retrieving-values)
        - [Retrieving fields as booleans](#retrieving-fields-as-booleans)
        - [Retrieving fields as datetimes](#retrieving-fields-as-datetimes)
    - [Array conversion](#array-conversion)
- [License](#license)
- [Change log](#change-log)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [About](#about)

Requirements
------------

[](#requirements)

- PHP 8.4 or 8.5
- Laravel 11 or 12

How to install
--------------

[](#how-to-install)

**Add the package** to your project using composer:

```
composer require sebastiaanluca/laravel-boolean-dates
```

**Set up your Eloquent model** by:

1. Adding your datetime columns to the `$casts` property or `casts()` method
2. Adding the boolean attributes to `$appends`
3. Creating attribute accessors and mutators for each field

```
