首页 > 解决方案 > 在 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 客户端的帮助下创建连接的方法,但我想学习一种新的方法。请帮忙

标签: phplaravelmongodbeloquent

解决方案


推荐阅读