需要安装数据库扩展:
composer require vlucas/phpdotenv ^5.1.0composer require illuminate/database ^8.0composer require illuminate/events # 监听sql语句需要这个,否则不需要
在 support/bootstrap/db/laravel.php 文件中添加sql监听语句
<?php/*** This file is part of webman.** Licensed under The MIT License* For full copyright and license information, please see the MIT-LICENSE.txt* Redistributions of files must retain the above copyright notice.** @author walkor<walkor@workerman.net>* @copyright walkor<walkor@workerman.net>* @link http://www.workerman.net/* @license http://www.opensource.org/licenses/mit-license.php MIT License*/namespace support\bootstrap\db;use support\bootstrap\Log;use Webman\Bootstrap;use Illuminate\Database\Capsule\Manager as Capsule;use Illuminate\Events\Dispatcher;use Illuminate\Container\Container;use Workerman\Worker;/*** Class Laravel* @package support\bootstrap\db*/class Laravel implements Bootstrap{/*** @param Worker $worker** @return void*/public static function start($worker){if (!class_exists('\Illuminate\Database\Capsule\Manager')) {return;}$capsule = new Capsule;$configs = config('database');$default_config = $configs['connections'][$configs['default']];$capsule->addConnection($default_config);foreach ($configs['connections'] as $name => $config) {$capsule->addConnection($config, $name);}if (class_exists('\Illuminate\Events\Dispatcher')) {$capsule->setEventDispatcher(new Dispatcher(new Container));}$capsule->setAsGlobal();# sql语句监听if(env('APP_DEBUG')){Capsule::connection()->listen(function ($query) {//这里是执行sql后的监听回调方法$sql = vsprintf(str_replace("?", "'%s'", $query->sql), $query->bindings) . " \t[" . $query->time . ' ms] ';// 把SQL写入到日志文件中echo $sql . PHP_EOL;Log::debug($sql);});}$capsule->bootEloquent();}}
