|
| 1 | +# Basic Usage |
| 2 | + |
| 3 | +Laravel Stateful Resources allows you to create dynamic API responses by changing the structure of your JSON resources based on different states. This is especially useful when you need to return different levels of detail for the same model depending on the context. |
| 4 | + |
| 5 | +## Generating a Stateful Resource |
| 6 | + |
| 7 | +The package provides an Artisan command to quickly generate a new stateful resource: |
| 8 | + |
| 9 | +```bash |
| 10 | +php artisan make:stateful-resource UserResource |
| 11 | +``` |
| 12 | + |
| 13 | +This command creates a new resource class in `app/Http/Resources/` that extends `StatefulJsonResource`: |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | + |
| 18 | +namespace App\Http\Resources; |
| 19 | + |
| 20 | +use Illuminate\Http\Request; |
| 21 | +use Farbcode\StatefulResources\StatefulJsonResource; |
| 22 | + |
| 23 | +class UserResource extends StatefulJsonResource |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Transform the resource into an array. |
| 27 | + * |
| 28 | + * @return array<string, mixed> |
| 29 | + */ |
| 30 | + public function toArray(Request $request): array |
| 31 | + { |
| 32 | + return parent::toArray($request); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Built-in States |
| 38 | + |
| 39 | +The package comes with three built-in states defined in the `State` enum: |
| 40 | + |
| 41 | +- **`Full`** - For all available attributes |
| 42 | +- **`Table`** - For attributes suitable for table/listing views |
| 43 | +- **`Minimal`** - For only essential attributes |
| 44 | + |
| 45 | +See the [Extending States](extending-states.md) documentation for how to configure this and add custom states. |
| 46 | + |
| 47 | +## Using States in Resources |
| 48 | + |
| 49 | +Inside your stateful resource, you can use conditional methods to control which attributes are included based on the current state: |
| 50 | + |
| 51 | +```php |
| 52 | +<?php |
| 53 | + |
| 54 | +namespace App\Http\Resources; |
| 55 | + |
| 56 | +use Farbcode\StatefulResources\Enums\State; |
| 57 | +use Farbcode\StatefulResources\StatefulJsonResource; |
| 58 | +use Illuminate\Http\Request; |
| 59 | + |
| 60 | +class UserResource extends StatefulJsonResource |
| 61 | +{ |
| 62 | + public function toArray(Request $request): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + 'id' => $this->id, |
| 66 | + 'name' => $this->name, |
| 67 | + 'email' => $this->whenState(State::Full, $this->email), |
| 68 | + 'profile' => $this->whenStateIn([State::Full], [ |
| 69 | + 'bio' => $this->bio, |
| 70 | + 'avatar' => $this->avatar, |
| 71 | + 'created_at' => $this->created_at, |
| 72 | + ]), |
| 73 | + 'role' => $this->whenStateIn([State::Full, State::Table], $this->role), |
| 74 | + 'last_login' => $this->unlessState(State::Minimal, $this->last_login_at), |
| 75 | + ]; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +You can also use the string representation of states instead of enum cases: |
| 81 | + |
| 82 | +```php |
| 83 | +'email' => $this->whenState('full', $this->email), |
| 84 | +'name' => $this->unlessState('minimal', $this->full_name), |
| 85 | +``` |
| 86 | + |
| 87 | +## Available Conditional Methods |
| 88 | + |
| 89 | +The package provides several methods to conditionally include attributes: |
| 90 | + |
| 91 | +### `whenState` |
| 92 | + |
| 93 | +Include a value only when the current state matches the specified state: |
| 94 | + |
| 95 | +```php |
| 96 | +'email' => $this->whenState(State::Full, $this->email), |
| 97 | +'admin_notes' => $this->whenState(State::Full, $this->admin_notes, 'N/A'), |
| 98 | +``` |
| 99 | + |
| 100 | +### `unlessState` |
| 101 | + |
| 102 | +Include a value unless the current state matches the specified state: |
| 103 | + |
| 104 | +```php |
| 105 | +'public_info' => $this->unlessState(State::Minimal, $this->public_information), |
| 106 | +``` |
| 107 | + |
| 108 | +### `whenStateIn` |
| 109 | + |
| 110 | +Include a value when the current state is one of the specified states: |
| 111 | + |
| 112 | +```php |
| 113 | +'detailed_info' => $this->whenStateIn([State::Full, State::Table], [ |
| 114 | + 'department' => $this->department, |
| 115 | + 'position' => $this->position, |
| 116 | +]), |
| 117 | +``` |
| 118 | + |
| 119 | +### `unlessStateIn` |
| 120 | + |
| 121 | +Include a value unless the current state is one of the specified states: |
| 122 | + |
| 123 | +```php |
| 124 | +'sensitive_data' => $this->unlessStateIn([State::Minimal, State::Table], $this->sensitive_info), |
| 125 | +``` |
| 126 | + |
| 127 | +### Magic Conditionals |
| 128 | + |
| 129 | +You can also use magic methods with for cleaner syntax: |
| 130 | + |
| 131 | +```php |
| 132 | +'email' => $this->whenStateFull($this->email), |
| 133 | +'name' => $this->unlessStateMinimal($this->full_name), |
| 134 | +``` |
| 135 | + |
| 136 | +## Using Stateful Resources |
| 137 | + |
| 138 | +### Setting the State Explicitly |
| 139 | + |
| 140 | +Use the static `state()` method to create a resource with a specific state: |
| 141 | + |
| 142 | +```php |
| 143 | +$user = UserResource::state(State::Minimal)->make($user); |
| 144 | +``` |
| 145 | + |
| 146 | +### Using Magic Methods |
| 147 | + |
| 148 | +You can also use magic methods for a more fluent syntax: |
| 149 | + |
| 150 | +```php |
| 151 | +// This is equivalent to the explicit state(State::Minimal) call |
| 152 | +$user = UserResource::minimal()->make($user); |
| 153 | +``` |
| 154 | + |
| 155 | +### Default State |
| 156 | + |
| 157 | +If no state is specified, the resource will use the default state. You can change the default state in the package's configuration file: `config/stateful-resources.php`. |
| 158 | + |
| 159 | +```php |
| 160 | +// Uses the default state |
| 161 | +$user = UserResource::make($user); |
| 162 | +``` |
0 commit comments