json-api-server/docs/create.md

977 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 (Request $request) {
    return $request->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 (Request $request) {
    return new CustomModel;
});

Events

onCreating

Run before the model is saved.

$type->onCreating(function ($model, Request $request) {
    // do something
});

onCreated

Run after the model is saved.

$type->onCreated(function ($model, Request $request) {
    // do something
});