@@ -17,6 +17,7 @@ composer require ark4ne/laravel-json-api
1717| ` describer.nullable ` | ` bool ` | For describer notation, defined if a value is nullable by default. |
1818| ` describer.date ` | ` string ` datetime format | For describer notation, defined default date time format. |
1919| ` describer.precision ` | ` int ` \ ` null ` | For describer notation, decimal precision for float value. ` null ` for disable rounding. |
20+ | ` describer.when-has ` | ` bool ` \ ` string[] ` | For describer notation, Apply automatically whenHas condition on attributes. |
2021| ` relationship.when-included ` | ` bool ` | Allow to disabled by default the loading of relationship data. |
2122
2223# Usage
@@ -398,9 +399,56 @@ UserResource::collection(User::all()); // => JsonApiCollection
398399| ` date ` | Cast to date, allow to use custom format |
399400| ` array ` | Cast to array |
400401| ` mixed ` | Don't cast, return as is |
402+ | ` enum ` | Get enum value. |
401403
402404### Relation methods
403405| Method | Description |
404406| ---------| -------------------------------------------------------------------|
405407| ` one ` | For relationship with a single value: ` HasOne ` , ` BelongsTo ` , ... |
406408| ` many ` | For relationship with many value: ` HasMany ` , ` BelongsToMany ` , ... |
409+
410+
411+ ### Enum
412+ Method ` enum ` allow to get enum value for backed enum or name for unit enum.
413+
414+ According to structure:
415+ ``` php
416+ /// Role.php
417+ enum Role {
418+ case ADMIN;
419+ case USER;
420+ }
421+ /// State.php
422+ enum State:int {
423+ case ACTIVE = 1;
424+ case INACTIVE = 0;
425+ }
426+ /// User.php
427+ class User extends Model
428+ {
429+ $casts = [
430+ 'role' => Role::class,
431+ 'state' => State::class,
432+ ];
433+ }
434+ ```
435+
436+ The following attributes resource:
437+ ``` php
438+ // UserResource.php
439+ protected function toAttributes(Request $request): array
440+ {
441+ return [
442+ 'status' => $this->enum(),
443+ 'role' => $this->enum(),
444+ ];
445+ }
446+ ```
447+
448+ Will return:
449+ ``` php
450+ [
451+ "status": 1,
452+ "role": "ADMIN"
453+ ]
454+ ```
0 commit comments