PHPackages                             samir-hussein/laravel-cqrs - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. samir-hussein/laravel-cqrs

ActiveLibrary[Framework](/categories/framework)

samir-hussein/laravel-cqrs
==========================

A clean CQRS (Command Query Responsibility Segregation) pattern implementation for Laravel applications

v1.0.0(4mo ago)015MITPHPPHP &gt;=8.0

Since Feb 20Pushed 3mo agoCompare

[ Source](https://github.com/samir-hussein/laravel-cqrs)[ Packagist](https://packagist.org/packages/samir-hussein/laravel-cqrs)[ RSS](/packages/samir-hussein-laravel-cqrs/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (7)Versions (2)Used By (0)

Laravel CQRS
============

[](#laravel-cqrs)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4b75e4900a393dbb42b3eef23c3a38b2af23f90d7dbdde768bb648b0aeb6f26b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d69722d6875737365696e2f6c61726176656c2d637172732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/samir-hussein/laravel-cqrs)[![License](https://camo.githubusercontent.com/a0bdf1f101a3d984e276da8b7cb4cb7fb9200a4725e787864e3155eaaac8c0fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73616d69722d6875737365696e2f6c61726176656c2d637172732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/samir-hussein/laravel-cqrs)

**CQRS (Command Query Responsibility Segregation) for Laravel** — separate writes (commands) from reads (queries), route both through a predictable pipeline (validation → middleware → handler), and resolve handlers automatically from naming conventions or explicit mappings.

This package gives you a **small, opinionated surface area**: two buses, one helper, convention-based handler resolution, optional Laravel validation on messages, and a **middleware pipeline** for cross-cutting behavior — without pulling in event sourcing or heavy infrastructure.

---

Table of contents
-----------------

[](#table-of-contents)

- [Why use this package](#why-use-this-package)
- [What you get](#what-you-get)
- [Requirements](#requirements)
- [Installation](#installation)
- [Concepts](#concepts)
- [Quick start](#quick-start)
- [Handler resolution](#handler-resolution)
- [The `CQRS` helper](#the-cqrs-helper)
- [Command and query base classes](#command-and-query-base-classes)
- [Validation](#validation)
- [Middleware pipeline](#middleware-pipeline)
- [Using the buses directly](#using-the-buses-directly)
- [Automatic request data (controllers)](#automatic-request-data-controllers)
- [Configuration reference](#configuration-reference)
- [Artisan commands](#artisan-commands)
- [Package architecture](#package-architecture)
- [Exceptions](#exceptions)
- [License](#license)
- [Changelog](#changelog)

---

Why use this package
--------------------

[](#why-use-this-package)

- **Clear boundaries**: Commands change state; queries return data. That separation scales with team size and keeps controllers thin.
- **One entry point**: `CQRS::dispatch()` works for both commands and queries — validate once, then run the pipeline.
- **Laravel-native**: Uses the container, config, validator, and optional controller injection of `Command` / `Query` instances.
- **Extensible without clutter**: Global and per-message middleware for logging, DB transactions, authorization, caching reads, etc.
- **Flexible routing to handlers**: Convention by default; override specific handlers via config when names or folders do not match.

---

What you get
------------

[](#what-you-get)

AreaDetails**Core types**`Command`, `Query` base classes with data access and validation hooks**Buses**`CommandBus`, `QueryBus` (singletons) resolving handlers and running the pipeline**Facade-style API**`LaravelCQRS\CQRS` static methods for dispatch with/without validation**Contracts**`CommandHandlerInterface`, `QueryHandlerInterface`, `MiddlewareInterface`**Pipeline**`Pipeline` executes middleware; middleware may be classes or container-resolved class names**Config**Namespaces, auto-resolve toggle, handler mappings, middleware stacks**Generators**`cqrs:command`, `cqrs:query`, `cqrs:handler`, `cqrs:middleware`**Exceptions**`HandlerNotFoundException`, `InvalidHandlerException`---

Requirements
------------

[](#requirements)

- PHP **≥ 8.0**
- Laravel **≥ 9** (`illuminate/*` packages as declared in `composer.json`)

---

Installation
------------

[](#installation)

```
composer require samir-hussein/laravel-cqrs
```

Laravel will auto-discover `LaravelCQRS\CQRSServiceProvider`.

Publish configuration (recommended for production apps):

```
php artisan vendor:publish --tag=cqrs-config
```

This creates `config/cqrs.php`.

---

Concepts
--------

[](#concepts)

- **Command**: An intent to change application state (create user, place order). Typically handled once, side effects allowed.
- **Query**: A read model request (get user by id, list products). No state change in the CQRS sense; return a result.
- **Handler**: A class that implements `CommandHandlerInterface` or `QueryHandlerInterface` and contains the use-case logic.
- **Flow**: `Controller → CQRS::dispatch() → [validate] → CommandBus/QueryBus → middleware pipeline → handler.handle() → result`

```
HTTP Request
    → Controller
        → CQRS::dispatch($message)  // validates if rules() non-empty
            → Bus
                → Middleware (global, then per-message)
                    → Handler::handle($message)
                        → Domain / repositories / models
                            → mixed result

```

---

Quick start
-----------

[](#quick-start)

### 1. Command + handler

[](#1-command--handler)

**Generate files:**

```
php artisan cqrs:command User/CreateUserCommand
php artisan cqrs:handler User/CreateUserCommandHandler --type=command
```

**Command** (`app/CQRS/Commands/User/CreateUserCommand.php`):

```
