尝试将mongodb与laravel连接
Laravel版本5.4.36
下载了软件包
composer require jenssegers/mongodb:3.2.0
jenssegers/mongodb-session
jenssegers/mongodb-sentry
config / database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'mydb',
'username' => 'usrename',
'password' => 'password',
],
Mongo数据库版本3.4.16
use mydb
db.createUser( { user: "username", pwd: "password", roles: [ "readWrite" ] } )
型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Hisnode extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'somecollection';
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mongomodel;
use Illuminate\Support\Facades\DB;
class ConnectmongoController extends Controller
{
public function index()
{
$node = Mongomodel::all();
}
}
当呼叫控制器出现错误时 验证失败。我不知道为什么会这样。我在mongo中创建一个用户,并输入该用户名和密码。