MongoDB AuthenticationException in InsertOne

271 views Asked by At

I'm using jessegers/mongodb to make the connection with Mongo DB and Laravel 5.4, but when I send the data to insert it gives me an authentication error.

my model has this:

protected $fillable = ['nombre','edad'];

my controller has

$usuario = new usuario;
$usuario->nombre =  $request->input('nombre');
$usuario->edad =  $request->input('edad');
$usuario->save();
$usuario = DB::collection('usuarios')->get();
return view ('informacion_entidad',compact('usuarios'));

and my view is like this

<form action="usuarios" method="post">
    <div class="form-group">
        <label for="nombre" class="control-label">nombre</label>
        <input id="nombre" name="nombre" placeholder="Ingrese el nombre" 
        type="text" class="form-control">
        <label for="edad" class="control-label">password</label>
        <input id="edad" name="edad" placeholder="Ingrese edad" 
        type="text" class="form-control">
        <button type="submit" class="btn btn-labeled btn-success">
        <span class="btn-label">
            <i class="fa fa-upload"></i>
        </span>ingresa</button>
    </div>
    <input type="hidden" name="_token" value="{!! csrf_token() !!}">
</form>

also have my database configuration like this:

        'default' => env('DB_CONNECTION', 'mongodb'),
        'mongodb' => [
            'driver'   => 'mongodb',
            'host'     => env('DB_HOST', 'localhost'),
            'port'     => env('DB_PORT', 27017),
            'database' => env('DB_DATABASE', 'prueba'),
            'username' => env('DB_USERNAME', 'admin'),
            'password' => env('DB_PASSWORD', 'admin2017'),
            'options' => [
                'database' => 'admin' // set the authentication database
            ]
        ],

and my .env file has:

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=prueba
DB_USERNAME=admin
DB_PASSWORD=admin2017
1

There are 1 answers

0
Steven Sparkies On

In this case in mongoDB configuration, there's

'options' => [
                'database' => 'admin'

and there shoud be

'options' => [
                'database' => 'prueba'

as I defined back in DB_DATABASE