雄辩的Laravel外部有jenssegers/laravel-mongodb多个数据库连接 [英] Eloquent Outside of Laravel with jenssegers/laravel-mongodb multiple DB connections

查看:361
本文介绍了雄辩的Laravel外部有jenssegers/laravel-mongodb多个数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Eloquent连接到默认数据库sqlserver和辅助连接的mongodb.我正在使用jenssegers/laravel-mongodb通过作曲家拉进来.这是我的数据库文件

I am trying to connect Eloquent to multiple databases sqlserver for the default and mongodb for the secondary connection. I am using jenssegers/laravel-mongodb pulled in using composer. Here is my database file

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
        'driver'   => 'sqlsrv',
        'host'     => '******',
        'database' => '*****',
        'username' => '*****',
        'password' => '*****',
        'prefix'   => '',
    ], 'default');

$capsule->addConnection([
        'driver'   => 'mongodb',
        'host'     => 'localhost',
        'port'     => 27017,
        'username' => '',
        'password' => '',
        'database' => 'production'
], 'mongo');

$capsule->setAsGlobal();
$capsule->bootEloquent();

问题是,当我尝试连接到mongo数据库时,它将引发以下错误:

The problem is when i try and connect to the mongo database it throws the following error:

InvalidArgumentException thrown with message "Unsupported driver [mongodb]"

在我看来,Illuminate连接工厂不支持开箱即用的mongodb,有人可以指出正确的方向来使它正常工作吗?

It looks to me that the Illuminate connection factory does not support mongodb out of the box, could someone please point me in the right direction to get this working?

推荐答案

您是对的,它没有本机支持.但这很容易添加:

You're right it does not have native support. But it's easy to add:

composer require jenssegers/mongodb:*

然后:

use Illuminate\Database\Capsule\Manager as Capsule;
use Jenssegers\Mongodb\Connection as Connection;

$capsule = new Capsule();

$capsule->getDatabaseManager()->extend('mongodb', function($config){
    return new Connection($config);
});

这篇关于雄辩的Laravel外部有jenssegers/laravel-mongodb多个数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆