@@ -73,7 +73,7 @@ class PostsResource extends EloquentResource
73
73
public function fields(): array
74
74
{
75
75
return [
76
- Field\Str ::make('title'),
76
+ Field\Attribute ::make('title'),
77
77
Field\ToOne::make('author')->type('users'),
78
78
];
79
79
}
@@ -124,11 +124,13 @@ requests, and force delete a resource using a `DELETE` request.
124
124
125
125
To expose the soft-delete capability to the client, add the
126
126
` Tobyz\JsonApiServer\Laravel\SoftDeletes ` trait to your Eloquent resource, and a
127
- nullable ` DateTime ` field to your fields array:
127
+ nullable [ ` DateTime ` field] ( attributes.md#date-and-datetime ) to your fields
128
+ array:
128
129
129
130
``` php
130
131
use Tobyz\JsonApiServer\Laravel\SoftDeletes; // [!code ++]
131
- use Tobyz\JsonApiServer\Schema\Field\DateTime; // [!code ++]
132
+ use Tobyz\JsonApiServer\Schema\Field\Attribute; // [!code ++]
133
+ use Tobyz\JsonApiServer\Schema\Type\DateTime; // [!code ++]
132
134
133
135
class PostsResource extends EloquentResource
134
136
{
@@ -139,15 +141,17 @@ class PostsResource extends EloquentResource
139
141
public function fields(): array
140
142
{
141
143
return [
142
- DateTime::make('deletedAt')->nullable(), // [!code ++]
144
+ Attribute::make('deletedAt') // [!code ++]
145
+ ->type(DateTime::make()), // [!code ++]
146
+ ->nullable(), // [!code ++]
143
147
];
144
148
}
145
149
}
146
150
```
147
151
148
152
If you prefer to use a boolean to indicate whether or not a resource is
149
153
soft-deleted instead of a nullable date-time value, you can use a
150
- ` BooleanDateTime ` field instead:
154
+ [ ` BooleanDateTime ` attribute ] ( attributes.md#booleandatetime ) instead:
151
155
152
156
``` php
153
157
use Tobyz\JsonApiServer\Schema\Field\BooleanDateTime;
@@ -290,7 +294,7 @@ rules to be applied to the value:
290
294
``` php
291
295
use function Tobyz\JsonApiServer\Laravel\rules;
292
296
293
- Str ::make('password')->validate(rules(['password']));
297
+ Attribute ::make('password')->validate(rules(['password']));
294
298
```
295
299
296
300
Note that values are validated one at a time, so interdependent rules such as
@@ -300,23 +304,17 @@ You can add an `{id}` placeholder to database rules such as `unique` which will
300
304
be substituted if a model is being updated:
301
305
302
306
``` php
303
- Str ::make('email')
304
- ->format('email')
307
+ Attribute ::make('email')
308
+ ->type(Str::make()-> format('email') )
305
309
->validate(Laravel\rules(['email', 'unique:users,email,{id}']));
306
310
```
307
311
308
312
Validating array contents is also supported:
309
313
310
314
``` php
311
- $type
312
- ->attribute('jobs')
313
- ->validate(
314
- Laravel\rules([
315
- 'required',
316
- 'array',
317
- '*' => ['string', 'min:3', 'max:255'],
318
- ]),
319
- );
315
+ Attribute::make('jobs')->validate(
316
+ Laravel\rules(['required', 'array', '*' => ['string', 'min:3', 'max:255']]),
317
+ );
320
318
```
321
319
322
320
You can also pass an array of custom messages and custom attribute names as the
0 commit comments