Skip to main content
Bulk operations allow you to create, update, or delete multiple records in a single request. Rest Generic Class provides built-in bulk support with automatic transaction handling and rollback on errors.

Why Bulk Operations?

Without bulk operations:
With bulk operations:
Performance gain: 25x faster

Bulk Create

Setup

Define the MODEL constant in your model:
Product.php

Single Create (Normal)

Response:

Bulk Create

Wrap records in an array with the model key:
Response:
The key must match the lowercase MODEL constant. For Product::MODEL = 'product', use {"product": [...]}.

Bulk Update

Route Setup

Register the updateMultiple route:
routes/api.php

Request Format

Each record must include the primary key:
Response:

Partial Updates

You don’t need to send all fields, only the ones you want to update:

Transaction Handling

All bulk operations are wrapped in database transactions.

Automatic Rollback

If any record fails, the entire transaction rolls back:
Response (422 Unprocessable Entity):
Result: ❌ Products 10 and 12 are NOT updated (transaction rolled back)
Bulk operations are all-or-nothing. One failure cancels the entire batch.

Commit on Success

If all records succeed, the transaction commits:

Validation in Bulk Operations

Per-Record Validation

Each record is validated individually:
Response:

Validation Rules

Define validation in your model or request:
ProductRequest.php
These rules apply to every record in a bulk operation.

Cache Invalidation

Bulk operations invalidate cache for the entire model.

How It Works

Efficiency: Cache version is bumped once per batch, not once per record.
All cached list_all and get_one queries for products are invalidated with a single version bump.

Performance Optimization

Benchmark: 100 Record Updates

Improvement: 28.8x faster

Best Practices

Do:
  • Use bulk operations for batches of 10+ records
  • Send only fields that need updating
  • Batch similar operations together
  • Use pagination for very large batches (1000+)
Don’t:
  • Use bulk operations for 1-2 records (overhead not worth it)
  • Send all fields when only one changed
  • Mix creates and updates in same batch
  • Send batches larger than 500 records without testing

Real-World Examples

Inventory Update (E-commerce)

Update stock levels from warehouse system:

Price Update (Admin Dashboard)

Apply discount to multiple products:

Status Batch Update (CRM)

Update lead status after import:

Bulk Create from Import (CSV Upload)

Error Handling

Partial Success Not Supported

Rest Generic Class does not support partial success in bulk operations. Either all records succeed or all fail. Why?
Partial success leads to inconsistent state and makes error recovery complex.
Alternative:
If you need partial success, send smaller batches:

Logging Errors

Bulk operation errors are logged automatically:
Check logs at storage/logs/rest-generic-class.log.

Testing Bulk Operations

Unit Test Example

Troubleshooting

”MODEL constant not defined” Error

Symptom: Bulk create fails with “MODEL not found” Cause: Missing MODEL constant in model Solution:

Wrong Key in Request

Symptom: Returns single-record response instead of bulk Cause: Key doesn’t match lowercase MODEL constant Fix:

Transaction Timeout

Symptom: Request times out with large batches Cause: Batch too large or slow validation Solution:

Memory Limit Exceeded

Symptom: “Allowed memory size exhausted” Cause: Loading too many models into memory Solution:

Next Steps

Caching

Understand cache invalidation with bulk operations

Many-to-Many

Bulk attach/detach operations for pivot tables

Performance

Advanced performance optimization techniques

Testing

Write integration tests for bulk endpoints

Evidence

  • File: src/Core/Services/BaseService.php
    Lines: 611-627, 664-676
    Implements create() (detects bulk), save_array(), and update_multiple()
  • File: src/Core/Controllers/RestController.php
    Lines: 158-176, 211-230
    Shows store() and updateMultiple() with transaction handling
  • File: src/Core/Models/BaseModel.php
    Lines: 28
    Defines MODEL constant requirement for bulk operations
  • File: README.md
    Lines: 181-194
    Shows bulk update example usage