在app/providers/AppServicesProviders.php 文件的 boot方法中增加如下代码即可

    1. <?php
    2. public function boot()
    3. {
    4. \DB::listen(function ($sql) {
    5. foreach ($sql->bindings as $i => $binding) {
    6. if ($binding instanceof \DateTime) {
    7. $sql->bindings[$i] = $binding->format('Y-m-d H:i:s');
    8. } else {
    9. if (is_string($binding)) {
    10. $sql->bindings[$i] = "'$binding'";
    11. }
    12. }
    13. }
    14. // Insert bindings into query
    15. $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
    16. $query = vsprintf($query, $sql->bindings);
    17. // Save the query to file
    18. $logFile = fopen(
    19. storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
    20. 'a+'
    21. );
    22. fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
    23. fclose($logFile);
    24. });
    25. }