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