Steve Boo
Steve Boo

Reputation: 63

How to insert data into MongoDB with Laravel

First I've set the default by setting the database connection to be mongodb.

'mongodb' => [
      'driver'   => 'mongodb',
      'host'     => 'localhost',
      'username' => 'dofUser',
      'password' => 'dofPass',
      'database' => 'dof',
  ],

Apps/models/user

   <?php
use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

    protected $collection = 'user';
    //$user = user::all();
}

and Router

        Route::post('/register', function()
{
   $user->model('User', 'App\Models\User');
   $user = new User;

 $user = User::create(array(
'username' => Input::get('username'),
'fullname' => Input::get('fullname'),
'password' => Hash::make(Input::get('password')),
'gender' => Input::get('gender'),
'month' => Input::get('month'),
'day' => Input::get('day'),
'year' => Input::get('year'),
'phone' => Input::get('phone'),
'email' => Input::get('email-act'),
'agree' => Input::get('agree')
));

    return redirect('/login');

});

But I submit the browser is blank. Not redirect to login in MongoDB I just make a Collection named user.

Upvotes: 1

Views: 7338

Answers (3)

IshaS
IshaS

Reputation: 837

You don't need to create User again. You can do like this.

 $user = new User();

 $user->'username' = Input::get('username');
 $user->'fullname' = Input::get('fullname');
 $user ->'password' = Hash::make(Input::get('password'));

 $user->save();

 return redirect('/login');

You can check the following tutorial for learn how to insert data to mongodb using Laravel.

Laravel with MongoDB CURD operations - Configuration

Laravel with MongoDB CURD operations - Save data

Check your login routes return something. Please post your method related to the login route.

Upvotes: 1

Damith
Damith

Reputation: 114

Please recheck "return redirect('/login');"

Upvotes: 0

user2617403
user2617403

Reputation: 397

you can check this tutorial in youtube https://www.youtube.com/watch?v=ltFSEUCCD_4

it teach how to add data into laravel by mongodb

Upvotes: 0

Related Questions