7

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..

2 Answers 2

10

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");
Sign up to request clarification or add additional context in comments.

8 Comments

Hi @Anand, Thank you for your quick response. I changed my code as per your instructions and I got this error message screencast.com/t/El6x69E9pC
and why you are writing this portion here,i should be in controller
I tried it and here's the result screencast.com/t/LzAXD6nBFN. Is there a documentation out there that teach how to use mongodb in laravel? Please advise.
Actually I'm a newbie in Laravel.. Please help.
I'm just following the instructions from this documentation github.com/jenssegers/Laravel-MongoDB
|
2

Its not a mongo+Laravel issue.The issue occurs because of the below code line inside model

$user = User::all();

So please rewrite the code as below

app/model/user.php

<?php 

    use Jenssegers\Mongodb\Model as Eloquent;

    class User extends Eloquent {

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

    }
?>

all() is a predefined function returns all rows of that model.so you can access it via without function definition.rewrite routes as below

app/routes.php

<?php 
    Route::get('users', function()
   {

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

   });
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.