@@ -18,15 +18,16 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui
18
18
19
19
## Available flags list
20
20
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 |
30
31
31
32
## How it works
32
33
@@ -81,32 +82,32 @@ class Post extends Model
81
82
82
83
#### Get only active models
83
84
84
- ``` shell
85
+ ``` php
85
86
Post::all();
86
87
Post::withoutInactive();
87
88
```
88
89
89
90
#### Get only inactive models
90
91
91
- ``` shell
92
+ ``` php
92
93
Post::onlyInactive();
93
94
```
94
95
95
96
#### Get active + inactive models
96
97
97
- ``` shell
98
+ ``` php
98
99
Post::withInactive();
99
100
```
100
101
101
102
#### Activate model
102
103
103
- ``` shell
104
+ ``` php
104
105
Post::where('id', 4)->activate();
105
106
```
106
107
107
108
#### Deactivate model
108
109
109
- ` ` ` shell
110
+ ``` php
110
111
Post::where('id', 4)->deactivate();
111
112
```
112
113
@@ -132,32 +133,32 @@ class Post extends Model
132
133
133
134
#### Get only accepted models
134
135
135
- ` ` ` shell
136
+ ``` php
136
137
Post::all();
137
138
Post::withoutUnaccepted();
138
139
```
139
140
140
141
#### Get only unaccepted models
141
142
142
- ` ` ` shell
143
+ ``` php
143
144
Post::onlyUnaccepted();
144
145
```
145
146
146
147
#### Get accepted + unaccepted models
147
148
148
- ` ` ` shell
149
+ ``` php
149
150
Post::withUnaccepted();
150
151
```
151
152
152
153
#### Accept model
153
154
154
- ` ` ` shell
155
+ ``` php
155
156
Post::where('id', 4)->accept();
156
157
```
157
158
158
159
#### Deactivate model
159
160
160
- ` ` ` shell
161
+ ``` php
161
162
Post::where('id', 4)->unaccept();
162
163
```
163
164
@@ -183,32 +184,32 @@ class Post extends Model
183
184
184
185
#### Get only approved models
185
186
186
- ` ` ` shell
187
+ ``` php
187
188
Post::all();
188
189
Post::withoutUnapproved();
189
190
```
190
191
191
192
#### Get only unapproved models
192
193
193
- ` ` ` shell
194
+ ``` php
194
195
Post::onlyUnapproved();
195
196
```
196
197
197
198
#### Get approved + unapproved models
198
199
199
- ` ` ` shell
200
+ ``` php
200
201
Post::withUnapproved();
201
202
```
202
203
203
204
#### Approve model
204
205
205
- ` ` ` shell
206
+ ``` php
206
207
Post::where('id', 4)->approve();
207
208
```
208
209
209
210
#### Unapprove model
210
211
211
- ` ` ` shell
212
+ ``` php
212
213
Post::where('id', 4)->unapprove();
213
214
```
214
215
@@ -234,32 +235,32 @@ class Post extends Model
234
235
235
236
#### Get only published models
236
237
237
- ` ` ` shell
238
+ ``` php
238
239
Post::all();
239
240
Post::withoutUnpublished();
240
241
```
241
242
242
243
#### Get only unpublished models
243
244
244
- ` ` ` shell
245
+ ``` php
245
246
Post::onlyUnpublished();
246
247
```
247
248
248
249
#### Get published + unpublished models
249
250
250
- ` ` ` shell
251
+ ``` php
251
252
Post::withUnpublished();
252
253
```
253
254
254
255
#### Publish model
255
256
256
- ` ` ` shell
257
+ ``` php
257
258
Post::where('id', 4)->publish();
258
259
```
259
260
260
261
#### Unpublish model
261
262
262
- ` ` ` shell
263
+ ``` php
263
264
Post::where('id', 4)->unpublish();
264
265
```
265
266
@@ -285,32 +286,32 @@ class Post extends Model
285
286
286
287
#### Get only verified models
287
288
288
- ` ` ` shell
289
+ ``` php
289
290
Post::all();
290
291
Post::withoutUnverified();
291
292
```
292
293
293
294
#### Get only unverified models
294
295
295
- ` ` ` shell
296
+ ``` php
296
297
Post::onlyUnverified();
297
298
```
298
299
299
300
#### Get verified + unverified models
300
301
301
- ` ` ` shell
302
+ ``` php
302
303
Post::withUnverified();
303
304
```
304
305
305
306
#### Verify model
306
307
307
- ` ` ` shell
308
+ ``` php
308
309
Post::where('id', 4)->verify();
309
310
```
310
311
311
312
#### Unverify model
312
313
313
- ` ` ` shell
314
+ ``` php
314
315
Post::where('id', 4)->unverify();
315
316
```
316
317
@@ -361,38 +362,38 @@ By default all records that have a `is_kept` equals to 0 will be excluded from y
361
362
362
363
#### Get only kept models
363
364
364
- ```shell
365
+ ``` php
365
366
Post::all();
366
367
Post::withoutUnkept();
367
368
```
368
369
369
370
#### Get only unkept models
370
371
371
- ```shell
372
+ ``` php
372
373
Post::onlyUnkept();
373
374
```
374
375
375
376
#### Get kept + unkept models
376
377
377
- ```shell
378
+ ``` php
378
379
Post::withUnkept();
379
380
```
380
381
381
382
#### Keep model
382
383
383
- ```shell
384
+ ``` php
384
385
Post::where('id', 4)->keep();
385
386
```
386
387
387
388
#### Unkeep model
388
389
389
- ```shell
390
+ ``` php
390
391
Post::where('id', 4)->unkeep();
391
392
```
392
393
393
394
#### Get unkept models which older than hours
394
395
395
- ```shell
396
+ ``` php
396
397
Post::onlyUnkeptOlderThanHours(4);
397
398
```
398
399
@@ -420,40 +421,91 @@ class Post extends Model
420
421
421
422
#### Get only not expired models
422
423
423
- ```shell
424
+ ``` php
424
425
Post::all();
425
426
Post::withoutExpired();
426
427
```
427
428
428
429
#### Get only expired models
429
430
430
- ```shell
431
+ ``` php
431
432
Post::onlyExpired();
432
433
```
433
434
434
435
#### Get expired + not expired models
435
436
436
- ```shell
437
+ ``` php
437
438
Post::withExpired();
438
439
```
439
440
440
441
#### Set expire flag to model
441
442
442
- ```shell
443
+ ``` php
443
444
Post::where('id', 4)->expire();
444
445
```
445
446
446
447
#### Remove expire flag from model
447
448
448
- ```shell
449
+ ``` php
449
450
Post::where('id', 4)->unexpire();
450
451
```
451
452
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
+
452
504
## Testing
453
505
454
506
Run the tests with:
455
507
456
- ```shell
508
+ ``` sh
457
509
vendor/bin/phpunit
458
510
```
459
511
0 commit comments