View MongoDB Data
Create a model and controller
Create a model called Movie
to represent data from the sample
movies
collection in your MongoDB database and the corresponding
resource controller by running the following command:
php artisan make:model Movie -cr
When the command completes, it outputs the following message:
INFO Model [app/Models/Movie.php] created successfully. INFO Controller [app/Http/Controllers/MovieController.php] created successfully.
Edit the model to use the Laravel Integration
Open the Movie.php
model in your app/Models
directory and
make the following edits:
Replace the
Illuminate\Database\Eloquent\Model
import withMongoDB\Laravel\Eloquent\Model
Specify
"mongodb"
in the$connection
field
The edited Movie.php
file contains the following code:
namespace App\Models; use MongoDB\Laravel\Eloquent\Model; class Movie extends Model { protected $connection = 'mongodb'; }
Add a controller function
Open the MovieController.php
file in your app/Http/Controllers
directory. Replace the show()
function with the
following code to retrieve results that match a
database query and render it in the view:
public function show() { return view('browse_movies', [ 'movies' => Movie::where('runtime', '<', 60) ->where('imdb.rating', '>', 8.5) ->orderBy('imdb.rating', 'desc') ->take(10) ->get() ]); }
Generate a view
Run the following command from the application root directory to create a view that displays movie data:
php artisan make:view browse_movies
After you run the command, it outputs the following message:
INFO View [resources/views/browse_movie.blade.php] created successfully.
Open the browse_movie.blade.php
view file in the resources/views
directory. Replace the contents with the following code and save the
changes:
<!DOCTYPE html> <html> <head> <title>Browse Movies</title> </head> <body> <h2>Movies</h2> @forelse ($movies as $movie) <p> Title: {{ $movie->title }}<br> Year: {{ $movie->year }}<br> Runtime: {{ $movie->runtime }}<br> IMDB Rating: {{ $movie->imdb['rating'] }}<br> IMDB Votes: {{ $movie->imdb['votes'] }}<br> Plot: {{ $movie->plot }}<br> </p> @empty <p>No results</p> @endforelse </body> </html>
Optionally, view your results as JSON documents
Rather than generating a view and editing the browse_movie.blade.php
file, you can
use the toJson()
method to display your results in JSON format.
Replace the show()
function with the following code to retrieve results and
return them as JSON documents:
public function show() { $results = Movie::where('runtime', '<', 60) ->where('imdb.rating', '>', 8.5) ->orderBy('imdb.rating', 'desc') ->take(10) ->get(); return $results->toJson(); }
View the movie data
Open the URL http://127.0.0.1:8000/browse_movies in your web browser. The page shows a list of movies and details about each of them.
Tip
You can run the php artisan route:list
command from your application
root directory to view a list of available routes.
Note
If you run into issues, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of the page.