How to use MongoDB in Laravel Framework

codepinoys picture codepinoys · Dec 11, 2014 · Viewed 41k times · Source

I'm having a problem using the MongoDB with Laravel framework. I used this Laravel-MongoDB

Here's the error I got

enter image description here

/app/model/User.php

<?php 

use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

     //protected $connection = 'mongodb';
     protected $collection = 'users';

     $user = User::all();

     public function all()
     {
        return $this->$user;
     }
}


?>

/app/routes.php

Route::get('users', function()
{

    $users = User::all();   
    return View::make('users')->with('users',$users);

});

/app/config/database.php

'mongodb' => array(
            'driver'   => 'mongodb',
            'host'     => 'localhost',
            'port'     => 27017,
            'username' => 'username',
            'password' => 'password',
            'database' => 'users'
        ),

I don't know what's wrong with my implementation. Please help me guys..

Answer

Anand Patel picture Anand Patel · Dec 11, 2014

i think its not an issue with mongo

you can`t declare local class variable like that .

please try this

<?php 

use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

     //protected $connection = 'mongodb';
     protected $collection = 'users';

}


?>

controller/UserController.php

class UserController extends \BaseController 
{
        public function all()
        {
             return User::all();
        }
}

routes.php

route::get("all-users","UserController@all");