1.2 KiB
1.2 KiB
Attributes
Define an attribute field on your resource using the attribute method.
$type->attribute('firstName');
By default, the attribute will read and write to the property on your model with the same name. (The Eloquent adapter will snake_case it automatically for you.) If you'd like it to correspond to a different property, use the property method:
$type->attribute('firstName')
->property('fname');
Getters
Use the get method to define custom retrieval logic for your attribute, instead of just reading the value straight from the model property.
use Psr\Http\Message\ServerRequestInterface as Request;
use Tobyz\JsonApiServer\Schema\Attribute;
$type->attribute('firstName')
->get(function ($model, Request $request, Attribute $attribute) {
return ucfirst($model->first_name);
});
::: tip If you're using Eloquent, you could also define attribute casts or accessors on your model to achieve a similar thing. However, the Request instance will not be available in this context. :::