laravel 取sql语句

时间:2022-03-09 14:46:10
 \DB::connection()->enableQueryLog();

some sql action...

$query = \DB::getQueryLog();
$lastQuery = end($query); dd($lastQuery);
php artisan tinker

DB::listen(function($sql){ var_dump($sql);  });

Inside AppServiceProvider.php add the following to the boot method:

use DB;
use Event; //.. public function boot()
{
if (env('APP_ENV') === 'local') {
DB::connection()->enableQueryLog();
}
if (env('APP_ENV') === 'local') {
Event::listen('kernel.handled', function ($request, $response) {
if ( $request->has('sql-debug') ) {
$queries = DB::getQueryLog();
dd($queries);
}
});
}
}