php - 在 laravel 中创建一个 mongoDB 连接
问题描述
我正在制作一个 logIn 控制器,我想在其中创建一个新的 mongo 连接,该连接在模型 findByEmail 中有一个方法
登录控制器.php
public function __construct()
{
// Creates mongo connection
$this->collection = ((new MongoDBClient())->__get(env('MONGODB_DATABASE'))->users);
}
型号:Users.php
class Users extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'users';
protected $attributes = [
'status' => 1
];
public function findByEmail($email)
{
$record = $this->where('email', $email)
->first();
return $record;
}
}
请指导我如何使用它创建连接。我知道一种在 mongoDB 客户端的帮助下创建连接的方法,但我想学习一种新的方法。请帮忙
解决方案
推荐阅读
- java - Apache POI - 查找列中最大字符长度的公式
- base64 - 如何解码/重新创建 Google 航班搜索 URL?
- javascript - 错误:'string' 类型的参数不可分配给'string[]' 类型的参数,必须找出 season_to_date
- javascript - html游戏脚本没有运行3行代码
- html - 列定位布局问题
- express - 我遇到了 ejs 无法使用 if else 语句编译的问题
- python - 上下文版本冲突错误,Microsoft Azure 机器学习工作室
- firebase - React Native + Firebase / NodeMailer - 可能未处理的承诺拒绝
- javascript - 如何在反应中将异步函数的值存储到全局变量中?
- connection - 从 uipath 到雪花的 OBDC 连接