Skip to content

[12.x] Clarify where the code should be placed #10423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025

Conversation

AhmedAlaa4611
Copy link
Contributor

Problem

The original example lacks context about where the DB::listen logic is expected to live, which may confuse developers unfamiliar with the typical usage patterns.

Proposed Fix

Add a comment to clarify that the DB::listen code should be placed in a service provider.

Reasoning

This small change improves readability by helping readers understand where the code belongs within a typical Laravel application structure.

@AhmedAlaa4611
Copy link
Contributor Author

I use the same tone that we are used in that section example:

// In our middleware...
Context::add('url', $request->url());
Context::add('trace_id', Str::uuid()->toString());

// In our controller...
ProcessPodcast::dispatch($podcast);

context.md Outdated
@@ -227,6 +227,7 @@ Stacks can be useful to capture historical information about a request, such as
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\DB;

// In our service provider...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you could use the filename code fence attribute:

```php filename=app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\DB;

DB::listen(function ($event) {
    Context::push('queries', [$event->time, $event->sql]);
});
```

@taylorotwell taylorotwell merged commit c5f1f47 into laravel:12.x May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants