Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

View MongoDB Data

1

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

Open the Movie.php model in your app/Models directory and make the following edits:

  • Replace the Illuminate\Database\Eloquent\Model import with MongoDB\Laravel\Eloquent\Model

  • Specify "mongodb" in the $connection field

The edited Movie.php file contains the following code:

<?php
namespace App\Models;
use MongoDB\Laravel\Eloquent\Model;
class Movie extends Model
{
protected $connection = 'mongodb';
}
3

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()
]);
}
4

Open the web.php file in the routes directory. Add an import for the MovieController and a route called browse_movies as shown in the following code:

<?php
// ...
use App\Http\Controllers\MovieController;
Route::get('/browse_movies/', [MovieController::class, 'show']);
5

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

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();
}
7

Run the following command from the application root directory to start your PHP built-in web server:

php artisan serve

After the server starts, it outputs the following message:

INFO Server running on [http://127.0.0.1:8000].
Press Ctrl+C to stop the server
8

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.

Next: Write Data

Back

Configure Your MongoDB Connection

Next

Write Data to MongoDB