json-api-server/docs/visibility.md

758 B
Raw Blame History

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:

$type->attribute('email')
    ->visible(function ($model, Context $context) {
        return $model->id === $request->getAttribute('userId');
    });

Hiding a field completely is useful when you want it the field to be available for writing but not reading for example, a password field.

$type->attribute('password')
    ->hidden()
    ->writable();