Chorus Actions
Chorus Actions are the core of Laravel Chorus’s write operations system. They provide a clean, powerful way to handle complex multi-table operations with built-in validation, offline support, and real-time synchronization.Core Concepts
What is a Chorus Action?
A Chorus Action is a server-side class that:- Handles multiple operations atomically across different tables
- Validates data using operation-specific validation rules
- Supports offline writes automatically with queue and sync
- Enables optimistic updates for instant UI feedback
- Generates TypeScript functions for type-safe client usage
Key Features
- Multi-table operations - Create, update, delete across multiple tables in one action
- Automatic offline support - No configuration needed
- Operation validation - Table.operation-specific validation rules
- Type generation - Auto-generated TypeScript functions
- Single operation shorthand - Simplified API for simple actions
- Optimistic updates - Instant UI feedback
Creating Chorus Actions
Generate with Artisan
app/Actions/ChorusActions/.
Basic Structure
Validation System
Operation-Specific Rules
Therules() method uses a table.operation format for precise validation:
Available Operations
create- Insert new recordsupdate- Modify existing recordsdelete- Remove records
Automatic UUID Generation
Chorus automatically generates UUIDs for create operations when theid field is missing:
Handling Operation Data
Chorus Actions use thegetOperations() method to retrieve operation data from the request:
The getOperations() Method
ThegetOperations(string $table, string $operation) method returns an array of operation data:
Handling Empty Operations
Always check if operations exist before processing:Authorization and Validation
Validate data and check permissions within your operations:Single Operation Shorthand
For actions with only one operation rule, clients can use simplified data format:Server-Side (Single Operation)
Client-Side Usage
Route Registration
Important: Chorus Actions must be registered as routes to be accessible from the client-side. Add your action routes toroutes/web.php with proper authentication:
Basic Route Registration
Route Naming Convention
The route path should match the action name used in the generated TypeScript functions:- Action Class:
CreateMessageAction - Route Path:
/api/actions/create-message - Generated Function:
createMessageAction() - Route Name:
chorus.create-message(optional but recommended)
Authentication & Authorization
Always protect Chorus Action routes with appropriate middleware:Multi-Tenancy Support
For multi-tenant applications, you can add tenant-specific middleware:Client-Side Integration
Generated Functions
Chorus automatically generates TypeScript functions for each action:React Example
Vue Example
Advanced Patterns
Authorization
Database Transactions
Complex Business Logic
Best Practices
1. Name Actions Descriptively
2. Keep Actions Focused
Each action should represent a single business operation, even if it touches multiple tables.3. Use Transactions for Consistency
Wrap related operations in database transactions to ensure data consistency.4. Validate Everything
Use comprehensive validation rules for each operation to prevent invalid data.5. Handle Authorization
Always check permissions before performing operations.Next Steps
Write Path
Understand the complete write path flow
React Integration
Use Chorus Actions in React components
Vue Integration
Use Chorus Actions in Vue components
Commands
Generate and manage Chorus Actions with Artisan
Chorus Actions provide a powerful, flexible system for handling complex write operations while maintaining real-time synchronization and offline support. The combination of multi-table operations, automatic validation, and type-safe client integration makes building sophisticated real-time applications straightforward and maintainable.