Laravel Action Classes: Single Responsibility
January 16, 2026
•
1 min read
•
33 views
Table of Contents
Single responsibility with actions:
Create Action Class
class CreateOrderAction
{
public function execute(array $data): Order
{
return DB::transaction(function () use ($data) {
$order = Order::create($data);
$order->items()->createMany($data['items']);
event(new OrderCreated($order));
return $order;
});
}
}
Use in Controller
public function store(Request $request, CreateOrderAction $action)
{
$order = $action->execute($request->validated());
return redirect()->route('orders.show', $order);
}
Benefits
- Reusable across controllers
- Easy to test
- Clear responsibility
- Simpler controllers
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!