Need help architecting or implementing a REST API? Book a call!

Guide

Batch Operations

Learn how to manage entities in batches.

The following batch operations are supported for both models and relations: store, update, destroy and restore.

Batch Store

The endpoint expects an object in request payload with resources array, where each item is an object representing a resource (e.g. a post) to create.

// (POST) https://myapp.com/api/posts/batch
{
    "resources" : [
        {
            "title" : "My Post 1",
            "body" : "Example body text"
        },
         {
            "title" : "My Post 2",
            "body" : "Example body text"
        }
    ]
}

Batch Update

The endpoint expects an object in request payload with resources object, where each key is a resource id and item is an object representing a resource (e.g. a post) to update.

// (PATCH) https://myapp.com/api/posts/batch
{
    "resources" : {
        "5" : {
            "title" : "My Post 1 (updated)",
            "body" : "Example updated body text"
        },
        "6": {
            "title" : "My Post 2",
            "body" : "Example body text"
        }
    }
}

Batch Delete

The endpoint expects an object in request payload with resources array, where each item is an id of resource (e.g. a post) to delete.

// (DELETE) https://myapp.com/api/posts/batch
{
    "resources" : [5,6]
}

Batch Restore

The endpoint expects an object in request payload with resources array, where each item is an id of resource (e.g. a post) to restore.

// (POST) https://myapp.com/api/posts/batch/restore
{
    "resources" : [5,6]
}

Disabling Batch Operations

Batch operations are enabled by default on all resources. If you would like to disable specific endpoints, you can use Laravel's except method:

Orion::resource('posts', PostsController::class)->except(['batchStore', 'batchUpdate']);

To disable all batch operations on a resource, you can use withoutBatch method:

Orion::resource('posts', PostsController::class)->withoutBatch();

Copyright © 2019-2024 Aleksei Zarubin