How to use MongoDB in Laravel Framework
Asked Answered
Z

2

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

Zelma answered 11/12, 2014 at 10:3 Comment(0)
F
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");
Footstool answered 11/12, 2014 at 10:14 Comment(8)
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/El6x69E9pCZelma
and why you are writing this portion here,i should be in controllerFootstool
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.Zelma
Actually I'm a newbie in Laravel.. Please help.Zelma
I'm just following the instructions from this documentation github.com/jenssegers/Laravel-MongoDBZelma
what you can do is create one user controller named UserController you can write that user retrieve part in controllerFootstool
Thank you very much! Also, how can I insert new record, remove and update using Laravel Framework, any advise? Many Thanks.Zelma
great and jensseger package for mongo provides almost same functionality like Laravel Elouent except some function and that are mention on page(you are refering)Footstool
F
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);

   });
?>
Factorize answered 18/4, 2016 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.