Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

While using MongoDB with the Laravel framework I am getting the following error:

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'

        ),

Can someone guide me on this?

1 Answer

0 votes
by (108k points)
edited by

There is nothing wrong with your MongoDB, I think you are declaring the local class variable in the wrong way.

Try to implement the following code:

<?php 

use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

     //protected $connection = 'mongodb';

     protected $collection = 'users';

}

?>

And in the controller/UserController.php:

Do you want to be a full-stack developer? Enroll in this Full Stack Certification to get started with your journey.

class UserController extends \BaseController 

{

        public function all()

        {

             return User::all();

        }

}

routes.php

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

Related questions

0 votes
1 answer
asked Feb 2, 2020 in Web Technology by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)

Browse Categories

...