Skip to content

Commit ca7eeaa

Browse files
Update readme
1 parent b86c440 commit ca7eeaa

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

README.md

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ User registration controller:
2323
$input = request()->input();
2424

2525
$user = User::create([
26-
'has_accepted_terms_and_conditions' => $input['terms'],
27-
'allows_data_processing' => $input['data_processing'],
28-
'has_agreed_to_something' => $input['something'],
26+
'has_accepted_terms' => $input['terms'],
27+
'is_subscribed_to_newsletter' => $input['newsletter'],
2928
]);
3029
```
3130

3231
Anywhere else in your code:
3332

3433
```php
3534
// true or false (boolean)
36-
$user->has_accepted_terms_and_conditions;
35+
$user->has_accepted_terms;
3736

3837
// 2018-05-10 16:24:22 (Carbon instance)
39-
$user->accepted_terms_and_conditions_at;
38+
$user->accepted_terms_at;
4039
```
4140

4241
## Table of contents
@@ -75,11 +74,13 @@ composer require sebastiaanluca/laravel-boolean-dates
7574

7675
1. Adding your datetime columns to `$casts`
7776
2. Adding the boolean attributes to `$appends`
78-
3. Creating attribute accessors mutators for each field
77+
3. Creating attribute accessors and mutators for each field
7978

8079
```php
8180
<?php
8281

82+
declare(strict_types=1);
83+
8384
use Illuminate\Database\Eloquent\Model;
8485
use SebastiaanLuca\BooleanDates\BooleanDateAttribute;
8586

@@ -92,8 +93,7 @@ class User extends Model
9293
*/
9394
protected $casts = [
9495
'accepted_terms_at' => 'immutable_datetime',
95-
'allowed_data_processing_at' => 'immutable_datetime',
96-
'subscribed_to_newsletter_at' => 'immutable_datetime',
96+
'subscribed_to_newsletter_at' => 'datetime',
9797
];
9898

9999
/**
@@ -103,21 +103,15 @@ class User extends Model
103103
*/
104104
protected $appends = [
105105
'has_accepted_terms',
106-
'has_allowed_data_processing',
107-
'has_subscribed_to_newsletter',
106+
'is_subscribed_to_newsletter',
108107
];
109108

110109
protected function hasAcceptedTerms(): Attribute
111110
{
112111
return BooleanDateAttribute::for('accepted_terms_at');
113112
}
114113

115-
protected function hasAllowedDataProcessing(): Attribute
116-
{
117-
return BooleanDateAttribute::for('allowed_data_processing_at');
118-
}
119-
120-
protected function hasSubscribedToNewsletter(): Attribute
114+
protected function isSubscribedToNewsletter(): Attribute
121115
{
122116
return BooleanDateAttribute::for('subscribed_to_newsletter_at');
123117
}
@@ -140,7 +134,6 @@ return new class extends Migration {
140134
{
141135
Schema::table('users', static function (Blueprint $table): void {
142136
$table->timestamp('accepted_terms_at')->nullable();
143-
$table->timestamp('allowed_data_processing_at')->nullable();
144137
$table->timestamp('subscribed_to_newsletter_at')->nullable();
145138
});
146139
}
@@ -152,34 +145,38 @@ return new class extends Migration {
152145

153146
### Saving dates
154147

155-
If a boolean date field's value is true, it'll be automatically converted to the current datetime:
148+
If 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.
156149

157150
```php
158151
$user = new User;
159152

160153
// Setting values explicitly
161-
$user->has_accepted_terms_and_conditions = true;
162-
$user->allows_data_processing = 'yes';
154+
$user->has_accepted_terms = true;
155+
$user->has_accepted_terms = 'yes';
156+
$user->has_accepted_terms = '1';
157+
$user->has_accepted_terms = 1;
163158

164159
// Or using attribute filling
165-
$user->fill(['has_agreed_to_something' => 1]);
160+
$user->fill(['is_subscribed_to_newsletter' => 'yes']);
166161

167162
$user->save();
168163
```
169164

170165
All fields should now contain a datetime similar to `2018-05-10 16:24:22`.
171166

172-
Note 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`.
167+
Note 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).
168+
169+
For example:
173170

174171
```php
175172
$user = new User;
176173

177-
$user->has_accepted_terms_and_conditions = true;
174+
$user->has_accepted_terms = true;
178175
$user->save();
179176

180177
// `accepted_terms_at` column will contain `2022-03-13 13:20:00`
181178

182-
$user->has_accepted_terms_and_conditions = true;
179+
$user->has_accepted_terms = true;
183180
$user->save();
184181

185182
// `accepted_terms_at` column will still contain the original `2022-03-13 13:20:00` date
@@ -192,13 +189,12 @@ Of course you can also remove the saved date and time, for instance if a user re
192189
```php
193190
$user = User::findOrFail(42);
194191

195-
$user->has_accepted_terms_and_conditions = false;
196-
// $user->has_accepted_terms_and_conditions = null;
197-
198-
$user->allows_data_processing = 0;
199-
// $user->allows_data_processing = '0';
200-
201-
$user->has_agreed_to_something = '';
192+
$user->has_accepted_terms = false;
193+
$user->has_accepted_terms = null;
194+
$user->has_accepted_terms = '0';
195+
$user->has_accepted_terms = 0;
196+
$user->has_accepted_terms = '';
197+
// $user->has_accepted_terms = null;
202198

203199
$user->save();
204200
```
@@ -214,9 +210,8 @@ Use a boolean field's defined _key_ to access its boolean value:
214210
```php
215211
$user = User::findOrFail(42);
216212

217-
$user->has_accepted_terms_and_conditions;
218-
219213
// true or false (boolean)
214+
$user->has_accepted_terms;
220215
```
221216

222217
#### Retrieving fields as datetimes
@@ -226,16 +221,16 @@ Use a boolean field's defined _value_ to explicitly access its (Carbon) datetime
226221
```php
227222
$user = User::findOrFail(42);
228223

229-
// 2018-05-10 16:24:22 (Carbon instance)
224+
// 2018-05-10 16:24:22 (Carbon or CarbonImmutable instance)
230225
$user->accepted_terms_at;
231226

232227
// null
233-
$user->accepted_processing_at;
228+
$user->is_subscribed_to_newsletter;
234229
```
235230

236231
### Array conversion
237232

238-
When converting a model to an array, all boolean fields ánd their datetimes will be included:
233+
When converting a model to an array, the boolean fields will be included if you've added them to the `$appends` array in your model.
239234

240235
```php
241236
$user = User::findOrFail(42);
@@ -246,12 +241,10 @@ $user->toArray();
246241
* Which will return something like:
247242
*
248243
* [
249-
* 'accepted_terms_at' => \Illuminate\Support\Carbon('2018-05-10 16:24:22'),
250-
* 'accepted_processing_at' => NULL,
251-
* 'agreed_to_something_at' => \Illuminate\Support\Carbon('2018-05-10 16:24:22'),
252-
* 'accepted_terms_and_conditions' => true,
253-
* 'allows_data_processing' => false,
254-
* 'agreed_to_something' => true,
244+
* 'accepted_terms_at' => \Carbon\CarbonImmutable('2018-05-10 16:24:22'),
245+
* 'subscribed_to_newsletter_at' => \Illuminate\Support\Carbon('2018-05-10 16:24:22'),
246+
* 'has_accepted_terms' => true,
247+
* 'is_subscribed_to_newsletter' => true,
255248
* ];
256249
*/
257250
```

0 commit comments

Comments
 (0)