@@ -11,19 +11,22 @@ This package provides a `HasStatuses` trait that, once installed on a model, all
11
11
12
12
``` php
13
13
// set a status
14
- $model->setStatus('pending' , 'needs verification');
14
+ $model->setStatus(ModelStatus::Pending , 'needs verification');
15
15
16
16
// set another status
17
- $model->setStatus('accepted' );
17
+ $model->setStatus(ModelStatus::Approved );
18
18
19
19
// specify a reason
20
- $model->setStatus('rejected' , 'My rejection reason');
20
+ $model->setStatus(ModelStatus::Rejected , 'My rejection reason');
21
21
22
- // get the current status
22
+ // get the current status model
23
23
$model->status(); // returns an instance of \Spatie\ModelStatus\Status
24
24
25
+ //get the current status
26
+ $model->status; //returns an instance of the status enum
27
+
25
28
// get the previous status
26
- $latestPendingStatus = $model->latestStatus('pending' );
29
+ $latestPendingStatus = $model->latestStatus(ModelStatus::Pending );
27
30
28
31
$latestPendingStatus->reason; // returns 'needs verification'
29
32
```
@@ -84,14 +87,31 @@ return [
84
87
85
88
## Usage
86
89
87
- Add the ` HasStatuses ` trait to a model you like to use statuses on.
90
+ Create The Enum that holds all of your available statuses for the model. \
91
+ The enum must be string backed
92
+
93
+ ``` php
94
+ enum ModelStatusEnum: string
95
+ {
96
+ case Pending = "pending";
97
+ case Approved = "approved";
98
+ case Rejected = "rejected";
99
+ }
100
+ ```
101
+
102
+ Add the ` HasStatuses ` trait to a model you like to use statuses on and implement the abstract function getStatusEnumClass. Inside, you must return the class name of the enum you desire to use with this model.
88
103
89
104
``` php
90
105
use Spatie\ModelStatus\HasStatuses;
91
106
92
107
class YourEloquentModel extends Model
93
108
{
94
109
use HasStatuses;
110
+
111
+ public static function getStatusEnumClass(): string
112
+ {
113
+ return ModelStatusEnum::class;
114
+ }
95
115
}
96
116
```
97
117
@@ -100,45 +120,41 @@ class YourEloquentModel extends Model
100
120
You can set a new status like this:
101
121
102
122
``` php
103
- $model->setStatus('status-name' );
123
+ $model->setStatus(ModelStatusEnum::Pending );
104
124
```
105
125
106
126
A reason for the status change can be passed as a second argument.
107
127
108
128
``` php
109
- $model->setStatus('status-name' , 'optional reason');
129
+ $model->setStatus(ModelStatusEnum::Approved , 'optional reason');
110
130
```
111
131
112
132
### Retrieving statuses
113
133
114
134
You can get the current status of model:
115
135
116
136
``` php
117
- $model->status; // returns a string with the name of the latest status
137
+ $model->status; // returns an enum instance of the latest status assigned to the model
118
138
119
139
$model->status(); // returns the latest instance of `Spatie\ModelStatus\Status`
120
140
121
141
$model->latestStatus(); // equivalent to `$model->status()`
122
142
```
123
143
124
- You can also get latest status of a given name :
144
+ You can also get latest status of a given value :
125
145
126
146
``` php
127
- $model->latestStatus('pending' ); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending `
147
+ $model->latestStatus(ModelStatusEnum::Approved ); // returns an instance of `Spatie\ModelStatus\Status` that has the value `approved `
128
148
```
129
- Get all available status names for the model.
130
149
131
- ``` php
132
- $statusNames = $model->getStatusNames(); // returns a collection of all available status names.
133
- ```
134
150
135
- The following examples will return statusses of type ` status 1 ` or ` status 2 ` , whichever is latest.
151
+ The following examples will return statusses of type Approved or Rejected , whichever is latest.
136
152
137
153
``` php
138
- $lastStatus = $model->latestStatus(['status 1', 'status 2' ]);
154
+ $lastStatus = $model->latestStatus([ModelStatusEnum::Approved, ModelStatusEnum::Rejected ]);
139
155
140
156
// or alternatively...
141
- $lastStatus = $model->latestStatus('status 1', 'status 2' );
157
+ $lastStatus = $model->latestStatus(ModelStatusEnum::Approved, ModelStatusEnum::Rejected );
142
158
```
143
159
144
160
All associated statuses of a model can be retrieved like this:
@@ -149,45 +165,45 @@ $allStatuses = $model->statuses;
149
165
This will check if the model has status:
150
166
151
167
``` php
152
- $model->setStatus('status1' );
168
+ $model->setStatus(ModelStatusEnum::Approved );
153
169
154
- $isStatusExist = $model->hasStatus('status1' ); // return true
155
- $isStatusExist = $model->hasStatus('status2' ); // return false
170
+ $isStatusExist = $model->hasStatus(ModelStatusEnum::Approved ); // return true
171
+ $isStatusExist = $model->hasStatus(ModelStatusEnum::Rejected ); // return false
156
172
```
157
173
### Retrieving models with a given latest state
158
174
159
- The ` currentStatus ` scope will return models that have a status with the given name .
175
+ The ` currentStatus ` scope will return models that have a status with the given value .
160
176
161
177
``` php
162
- $allPendingModels = Model::currentStatus('pending' );
178
+ $allPendingModels = Model::currentStatus(ModelStatusEnum::Approved );
163
179
164
180
//or array of statuses
165
- $allPendingModels = Model::currentStatus(['pending', 'initiated' ]);
166
- $allPendingModels = Model::currentStatus('pending', 'initiated' );
181
+ $allPendingModels = Model::currentStatus([ModelStatusEnum::Approved, ModelStatusEnum::Pending ]);
182
+ $allPendingModels = Model::currentStatus(ModelStatusEnum::Approved, ModelStatusEnum::Pending );
167
183
```
168
184
169
- ### Retrieving models without a given state
185
+ ### Retrieving models without a given status
170
186
171
187
The ` otherCurrentStatus ` scope will return all models that do not have a status with the given name, including any model that does not have any statuses associated with them.
172
188
173
189
``` php
174
- $allNonPendingModels = Model::otherCurrentStatus('pending' );
190
+ $allNonPendingModels = Model::otherCurrentStatus(ModelStatusEnum::Pending );
175
191
```
176
192
177
193
You can also provide an array of status names to exclude from the query.
178
194
``` php
179
- $allNonInitiatedOrPendingModels = Model::otherCurrentStatus(['initiated', 'pending' ]);
195
+ $allNonPendingOrRejectedModels = Model::otherCurrentStatus([ModelStatusEnum::Pending, ModelStatusEnum::Rejected ]);
180
196
181
197
// or alternatively...
182
- $allNonInitiatedOrPendingModels = Model::otherCurrentStatus('initiated', 'pending' );
198
+ $allNonPendingOrRejectedModels = Model::otherCurrentStatus(ModelStatusEnum::Pending, ModelStatusEnum::Rejected );
183
199
```
184
200
185
201
### Validating a status before setting it
186
202
187
203
You can add custom validation when setting a status by overriding the ` isValidStatus ` method:
188
204
189
205
``` php
190
- public function isValidStatus(string $name , ?string $reason = null): bool
206
+ public function isValidStatus($statusEnum , ?string $reason = null): bool
191
207
{
192
208
...
193
209
@@ -204,15 +220,15 @@ If `isValidStatus` returns `false` a `Spatie\ModelStatus\Exceptions\InvalidStatu
204
220
You may bypass validation with the ` forceSetStatus ` method:
205
221
206
222
``` php
207
- $model->forceSetStatus('invalid-status-name' );
223
+ $model->forceSetStatus(ModelStatusEnum::Pending );
208
224
```
209
225
210
226
### Check if status has been assigned
211
227
212
228
You can check if a specific status has been set on the model at any time by using the ` hasEverHadStatus ` method:
213
229
214
230
``` php
215
- $model->hasEverHadStatus('status 1' );
231
+ $model->hasEverHadStatus(ModelStatusEnum::Approved );
216
232
```
217
233
218
234
### Delete status from model
@@ -222,13 +238,13 @@ You can delete any given status that has been set on the model at any time by us
222
238
Delete single status from model:
223
239
224
240
``` php
225
- $model->deleteStatus('status 1' );
241
+ $model->deleteStatus(ModelStatusEnum::Rejected );
226
242
```
227
243
228
244
Delete multiple statuses from model at once:
229
245
230
246
``` php
231
- $model->deleteStatus(['status 1', 'status 2' ]);
247
+ $model->deleteStatus([ModelStatusEnum::Pending, ModelStatusEnum::Rejected ]);
232
248
```
233
249
234
250
### Events
0 commit comments