I am new to Laravel. I am using the Jenssegers Laravel library, an eloquent model and query builder with support for MongoDB.
In my database, I created the document below. I am trying to show on the browser the content of the document but I am not successful.
I'd appreciate some hints or help on how to retrieve and display the content of a MongoDB document on Laravel 4.
Thanks.
{
"_id" : ObjectId("537124d584142189174ce113"),
"username" : "usertest",
"password" : "passtest",
"email" : "test@email.com",
"school" : "College university",
"country" : "USA",
"state" : "Washington",
"city" : "Seattle"
}
This is the code I got down so far..
File: /app/models/User.php
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
/**
* The database table (collection) used by the model.
*
* @var string
*/
protected $collection = 'user';
$users = User::all();
public function all()
{
return $this->$users;
}
}
File: /app/routes.php
Route::get('users', function()
{
return View::make('users')->with('user',$users);
});
File: /app/views/users.blade.php
@extends('layout')
@section('content')
@foreach($users as $user)
<p>{{ $user->name }}</p>
@endforeach
@stop
File: /app/views/layout.blade.php
<html>
<body>
<h1>Laravel Quickstart</h1>
@yield('content')
</body>
</html>
maybe this is obvious, but if you wan to use users
variable in view:
@foreach($users as $user)
// some code
@endforeach
then in route (or controller) you should you should pass users
not user
:
// should be
return View::make('users')->with('users',$users);
// not
return View::make('users')->with('user',$users);
Also I would rather put User::all() in route/controller then in model, because like mentioned in comment by Muharrem Tigdemir - it is already declared in Eloquent. It would look something like this:
Route::get('users', function()
{
return View::make('users')->with('user', User::all());
});
Hope it helped.