I'm having a problem using the MongoDB with Laravel framework. I used this Laravel-MongoDB
Here's the error I got
/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..
Can I Use MongoDB With Laravel? Yes! In fact, MongoDB is a great choice for Laravel projects.
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.
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
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.
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");
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);
});
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With