0040-laravel-event-driven-architecture

Dispatch typed Laravel domain events to listeners and subscribers.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0040-laravel-event-driven-architecture
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: 0040-laravel-event-driven-architecture
Source: https://github.com/MrJmpl3/codex_____data_____configuration/tree/main/skills/0040-laravel-event-driven-architecture
Command: npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0040-laravel-event-driven-architecture

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It reduces tightly coupled application code by routing side effects (notifications, PDFs, indexing, auditing) through Laravel events and listeners instead of direct calls scattered across services.

Core Features & Use Cases

  • Event + Listener Structure: Use typed event objects (data carriers) and single-responsibility listener classes to keep domain logic clean.
  • Queued & Safer Dispatching: Run slow listeners asynchronously with ShouldQueue, optionally conditionally with shouldQueue(), and protect data consistency with ShouldQueueAfterCommit or $afterCommit = true.
  • Discovery, Subscribers, and Model Lifecycle Hooks: Rely on Laravel listener auto-discovery, use event subscribers for grouped handling, and dispatch events from model observers for lifecycle-driven workflows.

Quick Start

Ask an AI to generate a Laravel event, register an auto-discovered listener that sends a notification, and modify it to run queued after the database transaction commits.

Frequently Asked Questions about 0040-laravel-event-driven-architecture

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I decouple Laravel side effects like notifications and PDF generation from my core business logic?

You can decouple Laravel side effects by dispatching typed domain events and handling them in single-responsibility listener classes. This event-driven architecture routes notifications, auditing, and indexing away from your main services instead of using direct calls.

How do I queue Laravel event listeners safely without hitting database transaction inconsistencies?

To queue Laravel event listeners safely, implement ShouldQueue and use ShouldQueueAfterCommit or set $afterCommit = true. This guarantees the listener only runs after the database transaction successfully commits, preventing data consistency issues.

When should I use event subscribers instead of standard listeners in Laravel?

Use Laravel event subscribers when you need to group multiple event handling methods into a single class. Subscribers are ideal for managing related domain events, whereas standard listeners are better for single-responsibility side effects.

Can I trigger Laravel events automatically from model lifecycle changes?

Yes, you can trigger Laravel events automatically from model lifecycle changes by using model observers. Observers hook into lifecycle events and dispatch your typed domain events, keeping lifecycle-driven workflows decoupled from direct service calls.

What is the best way to handle slow tasks like PDF generation in a Laravel order lifecycle workflow?

The best way to handle slow tasks in a Laravel order lifecycle is to run them asynchronously with queued listeners. By implementing ShouldQueue on your listener classes, you can process PDF generation, auditing, and indexing without blocking the main request.

How do I conditionally queue a Laravel event listener only when specific conditions are met?

To conditionally queue a Laravel event listener, implement ShouldQueue and define a shouldQueue() method on the listener class. This method allows you to add conditional logic that determines whether the listener should actually be pushed onto the queue.