本文介绍了将MongoDB与Laravel 5.3一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel 5.3中使用MongoDB.为此,我找到了这个包:

I want to use MongoDB with Laravel 5.3. For that I have found this package:

https://github.com/jenssegers/laravel-mongodb

我也在我的机器上安装了MongoDB,并与Laravel建立了数据库连接.

I have setup MongoDB on my machine and database connection with Laravel too.

我还能够使用Laravel Eloquent and Query builder运行查询.

I'm also able to run queries using Laravel Eloquent and Query builder.

唯一的问题是身份验证.

The only issue is with authentication.

为了与Laravel一起使用此包,我们需要在每个模型中扩展其Eloquent类,例如:

In order to use this package with Laravel, we need to extend its Eloquent class in each model like:

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {}

但是Laravel 5.3提供的用户模型已经扩展了Authenticateable类,PHP不支持多重继承.那么,这里的解决方案是什么?

But User model provided by Laravel 5.3 already extends Authenticatable class and PHP doesn't support multiple inheritance. So, what is the solution here?

https://github.com/laravel/laravel/blob /master/app/User.php

我更喜欢不需要大量修改Laravel 5.3提供的用户模型的解决方案.

I would prefer a solution in which User model provided by Laravel 5.3 doesn't need to be modified much.

谢谢

Parth vora

Parth vora

推荐答案

我有5.2项目,并且auth系统在jenssegers/laravel-mongodb下工作正常.如果要使用密码提示,则应添加Jenssegers\Mongodb\Auth\PasswordResetServiceProvider.

I have 5.2 project and auth system works just fine with jenssegers/laravel-mongodb. You should add Jenssegers\Mongodb\Auth\PasswordResetServiceProvider if you're using password reminders.

https://github.com/jenssegers/laravel-mongodb#extensions

另外,这是我正在工作的User.php,也许会有所帮助:

Also, here's my working User.php, maybe it will help:

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Eloquent implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

这篇关于将MongoDB与Laravel 5.3一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:44