Skip to content

Commit 92b655a

Browse files
Update readme
1 parent 67f5ced commit 92b655a

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

README.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,60 @@ $user->accepted_terms_and_conditions_at;
7171
composer require sebastiaanluca/laravel-boolean-dates
7272
```
7373

74-
**Require the `HasBooleanDates` trait** in your Eloquent model, then add the `$booleanDates` field:
74+
**Set up your Eloquent model** by:
75+
76+
1. Adding your datetime columns to `$casts`
77+
2. Adding the boolean attributes to `$appends`
78+
3. Creating attribute accessors mutators for each field
7579

7680
```php
7781
<?php
7882

7983
use Illuminate\Database\Eloquent\Model;
80-
use SebastiaanLuca\BooleanDates\HasBooleanDates;
84+
use SebastiaanLuca\BooleanDates\BooleanDateAttribute;
8185

8286
class User extends Model
8387
{
84-
use HasBooleanDates;
85-
86-
protected array $booleanDates = [
87-
'has_accepted_terms_and_conditions' => 'accepted_terms_at',
88-
'allows_data_processing' => 'accepted_processing_at',
89-
'has_agreed_to_something' => 'agreed_to_something_at',
88+
/**
89+
* The attributes that should be cast to native types.
90+
*
91+
* @var array<string, string>
92+
*/
93+
protected $casts = [
94+
'accepted_terms_at' => 'immutable_datetime',
95+
'allowed_data_processing_at' => 'immutable_datetime',
96+
'subscribed_to_newsletter_at' => 'immutable_datetime',
97+
];
98+
99+
/**
100+
* The accessors to append to the model's array form.
101+
*
102+
* @var array<int, string>
103+
*/
104+
protected $appends = [
105+
'has_accepted_terms',
106+
'has_allowed_data_processing',
107+
'has_subscribed_to_newsletter',
90108
];
109+
110+
protected function hasAcceptedTerms(): Attribute
111+
{
112+
return BooleanDateAttribute::for('accepted_terms_at');
113+
}
114+
115+
protected function hasAllowedDataProcessing(): Attribute
116+
{
117+
return BooleanDateAttribute::for('allowed_data_processing_at');
118+
}
119+
120+
protected function hasSubscribedToNewsletter(): Attribute
121+
{
122+
return BooleanDateAttribute::for('subscribed_to_newsletter_at');
123+
}
91124
}
92125
```
93126

94-
To wrap up, create a **migration** to create a new table or alter your existing table to add the timestamp fields:
127+
Optionally, 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:
95128

96129
```php
97130
<?php
@@ -107,8 +140,8 @@ return new class extends Migration {
107140
{
108141
Schema::table('users', static function (Blueprint $table): void {
109142
$table->timestamp('accepted_terms_at')->nullable();
110-
$table->timestamp('accepted_processing_at')->nullable();
111-
$table->timestamp('agreed_to_something_at')->nullable();
143+
$table->timestamp('allowed_data_processing_at')->nullable();
144+
$table->timestamp('subscribed_to_newsletter_at')->nullable();
112145
});
113146
}
114147
};
@@ -136,6 +169,22 @@ $user->save();
136169

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

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`.
173+
174+
```php
175+
$user = new User;
176+
177+
$user->has_accepted_terms_and_conditions = true;
178+
$user->save();
179+
180+
// `accepted_terms_at` column will contain `2022-03-13 13:20:00`
181+
182+
$user->has_accepted_terms_and_conditions = true;
183+
$user->save();
184+
185+
// `accepted_terms_at` column will still contain the original `2022-03-13 13:20:00` date
186+
```
187+
139188
### Clearing saved values
140189

141190
Of course you can also remove the saved date and time, for instance if a user retracts their approval:

0 commit comments

Comments
 (0)