Challenge 1: Create an Event and Listener Pair — Possible Solution ==================================================================== # php artisan make:event UserRegistered # php artisan make:listener SendWelcomeEmail --event=UserRegistered // app/Events/UserRegistered.php namespace App\Events; use App\Models\User; use Illuminate\Foundation\Events\Dispatchable; class UserRegistered { use Dispatchable; public function __construct( public User $user ) {} } // app/Listeners/SendWelcomeEmail.php namespace App\Listeners; use App\Events\UserRegistered; use Illuminate\Support\Facades\Mail; class SendWelcomeEmail { public function handle(UserRegistered $event): void { Mail::to($event->user->email)->send( new \App\Mail\WelcomeEmail($event->user) ); } } // app/Http/Controllers/Auth/RegisteredUserController.php (Breeze-generated, from Chapter 1) use App\Events\UserRegistered; public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|confirmed', ]); $user = User::create([ 'name' => $validated['name'], 'email' => $validated['email'], 'password' => $validated['password'], // hashed automatically via the User model's cast ]); UserRegistered::dispatch($user); Auth::login($user); return redirect(route('dashboard')); } WHY THIS WORKS -------------- - UserRegistered carries a single typed public property, $user — any listener that type-hints UserRegistered $event in its handle() method gets full, typed access to $event->user, with autocomplete and static analysis support a loose sender/instance/kwargs signature wouldn't provide. - SendWelcomeEmail's class doesn't need to be registered anywhere manually — placing it in app/Listeners with a handle(UserRegistered $event) method is enough for modern Laravel's auto-discovery to connect it to the UserRegistered event automatically. - UserRegistered::dispatch($user) fires the event at EXACTLY this one line in the registration controller — no other code path (an admin creating a user directly, a database seeder, a script) triggers this event unless it also explicitly calls dispatch(), which is the "application-triggered, not automatic" property the chapter highlights.