Home API Tools Posts Hire Me About
Sign In Create Account
Laravel

Laravel Action Classes: Single Responsibility

January 16, 2026 1 min read 31 views

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
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!