How to use MongoDB in Laravel Framework

I am having a problem using MongoDB with a Laravel base. I used this Laravel-MongoDB

Here is the error I received

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 do not know what happened to my implementation. Please help me guys ..

+6
source share
2 answers

I think this is not a problem with mongo

you cannot declare a local class variable in this way.

try it

 <?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 "); 
+7
source

This is not a mongo + laravel problem. The problem is due to the line code below inside the model

 $user = User::all(); 

So rewrite the code below

application / 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 this model. So you can access it without defining a function. write routes below

application / routes.php

 <?php Route::get('users', function() { $users = User::all(); return View::make('users')->with('users',$users); }); ?> 
+2
source

Source: https://habr.com/ru/post/979446/


All Articles