Skip to content

Commit e2bb9cd

Browse files
Automatically detect a model's boolean date fields
1 parent 6a985e7 commit e2bb9cd

File tree

5 files changed

+29
-35
lines changed

5 files changed

+29
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

9+
### Added
10+
11+
- Automatically detect a model's boolean date fields
12+
913
### Changed
1014

1115
- Add support for Laravel 5.8 (requires PHP 7.2 or higher)

README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $user->has_accepted_terms_and_conditions;
4040
$user->accepted_terms_and_conditions_at;
4141

4242
/*
43-
* 2018-05-10 16:24:22 (string or Carbon instance)
43+
* 2018-05-10 16:24:22 (Carbon instance)
4444
*/
4545
```
4646

@@ -76,7 +76,7 @@ $user->accepted_terms_and_conditions_at;
7676
composer require sebastiaanluca/laravel-boolean-dates
7777
```
7878

79-
**Require the `HasBooleanDates` trait** in your Eloquent model, then add the `$booleanDates` and `$dates` (optional) fields:
79+
**Require the `HasBooleanDates` trait** in your Eloquent model, then add the `$booleanDates` field:
8080

8181
```php
8282
<?php
@@ -96,22 +96,9 @@ class User extends Model
9696
'allows_data_processing' => 'accepted_processing_at',
9797
'has_agreed_to_something' => 'agreed_to_something_at',
9898
];
99-
100-
/**
101-
* The attributes that should be mutated to dates.
102-
*
103-
* @var array
104-
*/
105-
protected $dates = [
106-
'accepted_terms_at',
107-
'accepted_processing_at',
108-
'agreed_to_something_at',
109-
];
11099
}
111100
```
112101

113-
Adding the boolean date fields to the `$dates` array is **optional**, but encouraged as it'll convert all your boolean datetimes to Carbon instances.
114-
115102
To wrap up, create a **migration** to create a new or alter your existing table and add the timestamp fields:
116103

117104
```php
@@ -210,7 +197,7 @@ $user = User::findOrFail(42);
210197
$user->accepted_terms_at;
211198

212199
/*
213-
* 2018-05-10 16:24:22 (string or Carbon instance)
200+
* 2018-05-10 16:24:22 (Carbon instance)
214201
*/
215202

216203
$user->accepted_processing_at;
@@ -233,7 +220,7 @@ $user->toArray();
233220
* Which will return something like:
234221
*
235222
* [
236-
* 'accepted_terms_at' => '2018-05-10 16:24:22',
223+
* 'accepted_terms_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
237224
* 'accepted_processing_at' => NULL,
238225
* 'agreed_to_something_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
239226
* 'accepted_terms_and_conditions' => true,

src/HasBooleanDates.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88

99
trait HasBooleanDates
1010
{
11+
/**
12+
* Initialize the trait.
13+
*/
14+
public function initializeHasBooleanDates() : void
15+
{
16+
$this->dates = array_unique(
17+
array_merge(
18+
$this->dates,
19+
array_values($this->getBooleanDates())
20+
)
21+
);
22+
}
23+
1124
/**
1225
* Convert the model's attributes to an array.
1326
*

tests/Feature/BooleanAttributeTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function it sets the date from a true boolean() : void
4848
$model->has_accepted_terms_and_conditions = true;
4949

5050
$this->assertSame(
51-
Carbon::now()->format('Y-m-d H:i:s'),
52-
$model->accepted_terms_at
51+
Carbon::now()->format('Y-m-d H:i'),
52+
$model->accepted_terms_at->format('Y-m-d H:i')
5353
);
5454
}
5555

@@ -63,8 +63,8 @@ public function it sets the date from a non empty string() : void
6363
$model->has_accepted_terms_and_conditions = 'yes';
6464

6565
$this->assertSame(
66-
Carbon::now()->format('Y-m-d H:i:s'),
67-
$model->accepted_terms_at
66+
Carbon::now()->format('Y-m-d H:i'),
67+
$model->accepted_terms_at->format('Y-m-d H:i')
6868
);
6969
}
7070

@@ -78,8 +78,8 @@ public function it sets the date from a positive integer value() : void
7878
$model->has_agreed_to_something = 1;
7979

8080
$this->assertSame(
81-
Carbon::now()->format('Y-m-d H:i:s'),
82-
$model->agreed_to_something_at
81+
Carbon::now()->format('Y-m-d H:i'),
82+
$model->agreed_to_something_at->format('Y-m-d H:i')
8383
);
8484
}
8585

@@ -93,8 +93,8 @@ public function it sets the date from a positive integer string value()
9393
$model->has_accepted_terms_and_conditions = '1';
9494

9595
$this->assertSame(
96-
Carbon::now()->format('Y-m-d H:i:s'),
97-
$model->accepted_terms_at
96+
Carbon::now()->format('Y-m-d H:i'),
97+
$model->accepted_terms_at->format('Y-m-d H:i')
9898
);
9999
}
100100

tests/resources/TestModel.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,4 @@ class TestModel extends Model
2424
'allows_data_processing' => 'accepted_processing_at',
2525
'has_agreed_to_something' => 'agreed_to_something_at',
2626
];
27-
28-
/**
29-
* The attributes that should be mutated to dates.
30-
*
31-
* @var array
32-
*/
33-
protected $dates = [
34-
'accepted_processing_at',
35-
'tested_at',
36-
];
3727
}

0 commit comments

Comments
 (0)