Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Action Classes Pattern

Organize business logic with single-responsibility action classes.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Create Action
  2. Use in Controller
  3. With Dependencies

Action classes pattern:

Create Action

php
class CreateUserAction
{
    public function execute(array $data): User
    {
        $user = User::create($data);
        event(new UserCreated($user));
        return $user;
    }
}

Use in Controller

php
public function store(Request $request, CreateUserAction $action)
{
    $user = $action->execute($request->validated());
    return redirect()->route('users.show', $user);
}

With Dependencies

php
class ProcessOrderAction
{
    public function __construct(
        private PaymentGateway $payment,
        private NotificationService $notifications
    ) {}

    public function execute(Order $order): void
    {
        $this->payment->charge($order);
        $this->notifications->orderConfirmed($order);
    }
}
All articles

Keep reading

  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read
  • Laravel API Resources Transformation16 January 20261 min read