$databaseConfig = config("database.connections.mysql");
$pdo = (new ConnectionFactory(app()))->createConnector($databaseConfig)->connect($databaseConfig);
//设置查询数据方式为数组
Event::listen ( StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);
});
$mysqlConnection = new \Illuminate\Database\MySqlConnection($pdo, config("database.connections.mysql.database"), '', config("database.connections.mysql"));
$dbBuilder = $mysqlConnection->table($table);
if (app()->bound('events')) {
//注册查询事件
$mysqlConnection->setEventDispatcher(app()['events']);
}
有时候需要重写laravel的查询连接,如在Swoole下使用,可以用上面的方法实现