json-api-server/docs/visibility.md

23 lines
772 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Field Visibility
Restrict the visibility of a field using the `visible` and `hidden` methods.
You can optionally supply a closure to these methods which will receive the model instance, and should return a boolean value.
For example, the following schema will make an email attribute that only appears when the authenticated user is viewing their own profile:
```php
$type->attribute('email')
->visible(function ($model, Context $context) {
return $model->id === $context->getRequest()->getAttribute('userId');
});
```
Hiding a field completely is useful when you want it the field to be available for [writing](writing.md) but not reading for example, a password field.
```php
$type->attribute('password')
->hidden()
->writable();
```