Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MongoDB in Laravel Framework

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

like image 847
codepinoys Avatar asked Dec 11 '14 10:12

codepinoys


People also ask

Can I use MongoDB in laravel?

Can I Use MongoDB With Laravel? Yes! In fact, MongoDB is a great choice for Laravel projects.

Can laravel use NoSQL?

Edit: If MongoDB isn't an option, does Laravel support any other NoSQL by default? No, not by default. You need to use a package.

What is Jenssegers MongoDB?

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Can I connect PHP with MongoDB?

You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.


2 Answers

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");
like image 164
Anand Patel Avatar answered Oct 13 '22 12:10

Anand Patel


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);

   });
?>
like image 26
jisna Avatar answered Oct 13 '22 12:10

jisna