@@ -18,15 +18,16 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui
1818
1919## Available flags list
2020
21- | Trait name | Database columns | Flag type | Logic |
22- | ---------- | ---------------- | --------- | ----- |
23- | ` HasAcceptedFlag ` | ` is_accepted ` | Boolean | Classic |
24- | ` HasActiveFlag ` | ` is_active ` | Boolean | Classic |
25- | ` HasApprovedFlag ` | ` is_approved ` | Boolean | Classic |
26- | ` HasExpiredInverseFlag ` | ` is_expired ` | Boolean | Inverse |
27- | ` HasKeptFlag ` | ` is_kept ` | Boolean | Classic |
28- | ` HasPublishedFlag ` | ` is_published ` | Boolean | Classic |
29- | ` HasVerifiedFlag ` | ` is_verified ` | Boolean | Classic |
21+ | Trait name | Logic | Database columns | Flag type |
22+ | ---------- | ----- | ---------------- | --------- |
23+ | ` HasAcceptedFlag ` | Classic | ` is_accepted ` | Boolean |
24+ | ` HasActiveFlag ` | Classic | ` is_active ` | Boolean |
25+ | ` HasApprovedFlag ` | Classic | ` is_approved ` | Boolean |
26+ | ` HasClosedFlag ` | Inverse | ` is_closed ` | Boolean |
27+ | ` HasExpiredFlag ` | Inverse | ` is_expired ` | Boolean |
28+ | ` HasKeptFlag ` | Classic | ` is_kept ` | Boolean |
29+ | ` HasPublishedFlag ` | Classic | ` is_published ` | Boolean |
30+ | ` HasVerifiedFlag ` | Classic | ` is_verified ` | Boolean |
3031
3132## How it works
3233
@@ -81,32 +82,32 @@ class Post extends Model
8182
8283#### Get only active models
8384
84- ``` shell
85+ ``` php
8586Post::all();
8687Post::withoutInactive();
8788```
8889
8990#### Get only inactive models
9091
91- ``` shell
92+ ``` php
9293Post::onlyInactive();
9394```
9495
9596#### Get active + inactive models
9697
97- ``` shell
98+ ``` php
9899Post::withInactive();
99100```
100101
101102#### Activate model
102103
103- ``` shell
104+ ``` php
104105Post::where('id', 4)->activate();
105106```
106107
107108#### Deactivate model
108109
109- ` ` ` shell
110+ ``` php
110111Post::where('id', 4)->deactivate();
111112```
112113
@@ -132,32 +133,32 @@ class Post extends Model
132133
133134#### Get only accepted models
134135
135- ` ` ` shell
136+ ``` php
136137Post::all();
137138Post::withoutUnaccepted();
138139```
139140
140141#### Get only unaccepted models
141142
142- ` ` ` shell
143+ ``` php
143144Post::onlyUnaccepted();
144145```
145146
146147#### Get accepted + unaccepted models
147148
148- ` ` ` shell
149+ ``` php
149150Post::withUnaccepted();
150151```
151152
152153#### Accept model
153154
154- ` ` ` shell
155+ ``` php
155156Post::where('id', 4)->accept();
156157```
157158
158159#### Deactivate model
159160
160- ` ` ` shell
161+ ``` php
161162Post::where('id', 4)->unaccept();
162163```
163164
@@ -183,32 +184,32 @@ class Post extends Model
183184
184185#### Get only approved models
185186
186- ` ` ` shell
187+ ``` php
187188Post::all();
188189Post::withoutUnapproved();
189190```
190191
191192#### Get only unapproved models
192193
193- ` ` ` shell
194+ ``` php
194195Post::onlyUnapproved();
195196```
196197
197198#### Get approved + unapproved models
198199
199- ` ` ` shell
200+ ``` php
200201Post::withUnapproved();
201202```
202203
203204#### Approve model
204205
205- ` ` ` shell
206+ ``` php
206207Post::where('id', 4)->approve();
207208```
208209
209210#### Unapprove model
210211
211- ` ` ` shell
212+ ``` php
212213Post::where('id', 4)->unapprove();
213214```
214215
@@ -234,32 +235,32 @@ class Post extends Model
234235
235236#### Get only published models
236237
237- ` ` ` shell
238+ ``` php
238239Post::all();
239240Post::withoutUnpublished();
240241```
241242
242243#### Get only unpublished models
243244
244- ` ` ` shell
245+ ``` php
245246Post::onlyUnpublished();
246247```
247248
248249#### Get published + unpublished models
249250
250- ` ` ` shell
251+ ``` php
251252Post::withUnpublished();
252253```
253254
254255#### Publish model
255256
256- ` ` ` shell
257+ ``` php
257258Post::where('id', 4)->publish();
258259```
259260
260261#### Unpublish model
261262
262- ` ` ` shell
263+ ``` php
263264Post::where('id', 4)->unpublish();
264265```
265266
@@ -285,32 +286,32 @@ class Post extends Model
285286
286287#### Get only verified models
287288
288- ` ` ` shell
289+ ``` php
289290Post::all();
290291Post::withoutUnverified();
291292```
292293
293294#### Get only unverified models
294295
295- ` ` ` shell
296+ ``` php
296297Post::onlyUnverified();
297298```
298299
299300#### Get verified + unverified models
300301
301- ` ` ` shell
302+ ``` php
302303Post::withUnverified();
303304```
304305
305306#### Verify model
306307
307- ` ` ` shell
308+ ``` php
308309Post::where('id', 4)->verify();
309310```
310311
311312#### Unverify model
312313
313- ` ` ` shell
314+ ``` php
314315Post::where('id', 4)->unverify();
315316```
316317
@@ -361,38 +362,38 @@ By default all records that have a `is_kept` equals to 0 will be excluded from y
361362
362363#### Get only kept models
363364
364- ```shell
365+ ``` php
365366Post::all();
366367Post::withoutUnkept();
367368```
368369
369370#### Get only unkept models
370371
371- ```shell
372+ ``` php
372373Post::onlyUnkept();
373374```
374375
375376#### Get kept + unkept models
376377
377- ```shell
378+ ``` php
378379Post::withUnkept();
379380```
380381
381382#### Keep model
382383
383- ```shell
384+ ``` php
384385Post::where('id', 4)->keep();
385386```
386387
387388#### Unkeep model
388389
389- ```shell
390+ ``` php
390391Post::where('id', 4)->unkeep();
391392```
392393
393394#### Get unkept models which older than hours
394395
395- ```shell
396+ ``` php
396397Post::onlyUnkeptOlderThanHours(4);
397398```
398399
@@ -420,40 +421,91 @@ class Post extends Model
420421
421422#### Get only not expired models
422423
423- ```shell
424+ ``` php
424425Post::all();
425426Post::withoutExpired();
426427```
427428
428429#### Get only expired models
429430
430- ```shell
431+ ``` php
431432Post::onlyExpired();
432433```
433434
434435#### Get expired + not expired models
435436
436- ```shell
437+ ``` php
437438Post::withExpired();
438439```
439440
440441#### Set expire flag to model
441442
442- ```shell
443+ ``` php
443444Post::where('id', 4)->expire();
444445```
445446
446447#### Remove expire flag from model
447448
448- ```shell
449+ ``` php
449450Post::where('id', 4)->unexpire();
450451```
451452
453+ ### Setup a closable model
454+
455+ ``` php
456+ <?php
457+
458+ namespace App\Models;
459+
460+ use Cog\Flag\Traits\Inverse\HasClosedFlag;
461+ use Illuminate\Database\Eloquent\Model;
462+
463+ class Post extends Model
464+ {
465+ use HasClosedFlag;
466+ }
467+ ```
468+
469+ * Model must have boolean ` is_closed ` column in database table.*
470+
471+ ### Available functions
472+
473+ #### Get only not closed models
474+
475+ ``` php
476+ Post::all();
477+ Post::withoutClosed();
478+ ```
479+
480+ #### Get only closed models
481+
482+ ``` php
483+ Post::onlyClosed();
484+ ```
485+
486+ #### Get closed + not closed models
487+
488+ ``` php
489+ Post::withClosed();
490+ ```
491+
492+ #### Set close flag to model
493+
494+ ``` php
495+ Post::where('id', 4)->close();
496+ ```
497+
498+ #### Remove close flag from model
499+
500+ ``` php
501+ Post::where('id', 4)->unclose();
502+ ```
503+
452504## Testing
453505
454506Run the tests with:
455507
456- ```shell
508+ ``` sh
457509vendor/bin/phpunit
458510```
459511
0 commit comments