Pail regenerate session when DB::listen is used
Laravel Package Version
1.2.1
Laravel Version
11.33.2
PHP Version
8.3.13
Database Driver & Version
No response
Description
I would like to use Pail to monitor in real time the queries that are being executed in my web system. Following documentation in: https://laravel.com/docs/11.x/database#listening-for-query-events
<?php
namespace App\Providers;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
//
}
public function boot(): void
{
if ($this->app->isLocal()) {
Log::debug('Starting DB Logging...');
DB::listen(function (QueryExecuted $query) {
Log::debug('Query: {query} | Time: {time}.', [
'query' => $query->toRawSql(),
'time' => $query->time,
]);
});
}
}
}
The session id is changed during any request and the web server sometimes returns an HTTP No Content, Investigating the case in the code of Pail version 1.2.1, on Handler.php file lines 95 and 96 we have: https://github.com/laravel/pail/blob/v1.2.1/src/Handler.php#L95 https://github.com/laravel/pail/blob/v1.2.1/src/Handler.php#L96
Changing
default => [
'type' => 'http',
'method' => request()->method(),
'path' => request()->path(),
'auth_id' => Auth::id(),
'auth_email' => Auth::user() instanceof User ? Auth::user()->email : null, // @phpstan-ignore property.notFound
]
By
default => [
'type' => 'http',
'method' => request()->method(),
'path' => request()->path(),
'auth_id' => '',
'auth_email' => '', // @phpstan-ignore property.notFound
]
The problem is gone.
According to our friend Gonçalo in Issue 40 (https://github.com/laravel/pail/issues/40) these lines make new queries in the database, infinitely activating the handler DB ::listen.
If this is the correct way to monitor queries in real time using Pail, I believe this should be a bug that should be investigated.
Steps To Reproduce
Any new Laravel projects with use DB::listen according with documentation and have Log:: inside to print queries.
Thank you for reporting this issue!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Thank you!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
We're closing this for now due inactivity or progress. However, a pull request fixing the issue you have described, along with corresponding tests is always welcome. All pull requests are promptly reviewed by the Laravel team.
Thank you!