json-api-server/docs/create.md

1007 B

Creating Resources

You can allow resources to be created using the creatable and notCreatable methods on the schema builder.

Optionally pass a closure that returns a boolean value.

$type->creatable();

$type->creatable(function (Context $context) {
    return $context->getRequest()->getAttribute('user')->isAdmin();
});

Customizing the Model

When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the newModel method:

$type->newModel(function (Context $context) {
    return new CustomModel;
});

Events

onCreating

Run before the model is saved.

$type->onCreating(function (&$model, Context $context) {
    // do something
});

onCreated

Run after the model is saved.

$type->onCreated(function (&$model, Context $context) {
    $context->meta('foo', 'bar');
});