安装mongodb依赖
composer require jenssegers/mongodb:3.3
添加数据库连接配置
'mongodb' => [
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => '',
'password' => '',
],
添加mongodb service provider类
Jenssegers\Mongodb\MongodbServiceProvider::class,
添加Facade
'Mongo' => Jenssegers\Mongodb\MongodbServiceProvider::class,
创建MongodbController控制器
php artisan make:controller MongodbController
实现测试方法,访问users表中的数据
class MongodbController extends Controller
{
public function test1() {
$users = DB::connection('mongodb')
->collection('users')
->where('name', '=', 'lisi')
->get();
return json_encode($users);
}
}