Laravel 5.3 - How to log all queries on a page?
Here comes the perfect example:
https://laravel.com/docs/5.3/database#listening-for-query-events
Open app\Providers\AppServiceProvider.php and add the following to Boot()
function:
DB::listen(function ($query) {
var_dump([
$query->sql,
$query->bindings,
$query->time
]);
});
You can add this to the Providers/AppServiceProvider.php file and check them in the laravel log file with tail:
tail -f storage/logs/laravel.log
You can even filter with queries you want to log. For example, here I was using Laravel Passport, and didn't want to log all the oauth queries.
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
public function register() {
if (App::environment('local') && env('APP_URL') == 'http://localhost') {
Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
// filter oauth ones
if (!str_contains($query->sql, 'oauth')) {
Log::debug($query->sql . ' - ' . serialize($query->bindings));
}
});
}
}