|
| 1 | +.. laravel-quick-start-view-data: |
| 2 | + |
| 3 | +======================== |
| 4 | +View Sample MongoDB Data |
| 5 | +======================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: test connection, runnable, code example |
| 13 | + |
| 14 | +Follow the steps in this section to create the |
| 15 | +To create a webpage that displays data from your database, you need |
| 16 | +to create a model, view, and controller. |
| 17 | + |
| 18 | +.. procedure:: |
| 19 | + :style: connected |
| 20 | + |
| 21 | + .. step:: Create a Sample Model and Controller |
| 22 | + |
| 23 | + Create a model called ``Movie`` to access data from the sample ``movies`` |
| 24 | + collection in your MongoDB databse. You can create the model with a |
| 25 | + corresponding resource controller by running the following command: |
| 26 | + |
| 27 | + .. code-block:: bash |
| 28 | + |
| 29 | + php artisan make:model Movie -cr |
| 30 | + |
| 31 | + When the command successfully completes, you should see the following |
| 32 | + output: |
| 33 | + |
| 34 | + .. code-block:: none |
| 35 | + :copyable: false |
| 36 | + |
| 37 | + INFO Model [app/Models/Movie.php] created successfully. |
| 38 | + |
| 39 | + INFO Controller [app/Http/Controllers/MovieController.php] created successfully. |
| 40 | + |
| 41 | + .. step:: Edit the Model to use {+odm-short+} |
| 42 | + |
| 43 | + Navigate to the ``app/Models`` directory and open the ``Movie.php`` file. |
| 44 | + Edit the following information in the file: |
| 45 | + |
| 46 | + - Replace the ``Illuminate\Database\Eloquent\Model`` import with ``MongoDB\Laravel\Eloquent\Model`` |
| 47 | + - Specify ``mongodb`` in the ``$connection`` field |
| 48 | + |
| 49 | + |
| 50 | + Your ``Movie.php`` file should contain the following code: |
| 51 | + |
| 52 | + .. code-block:: php |
| 53 | + |
| 54 | + <?php |
| 55 | + |
| 56 | + namespace App\Models; |
| 57 | + |
| 58 | + use MongoDB\Laravel\Eloquent\Model; |
| 59 | + |
| 60 | + class Movie extends Model |
| 61 | + { |
| 62 | + protected $connection = 'mongodb'; |
| 63 | + } |
| 64 | + |
| 65 | + .. step:: Add a Controller Function |
| 66 | + |
| 67 | + Navigate to the ``app/Http/Controllers`` directory and open the |
| 68 | + ``MovieController.php`` file. Replace the ``show()`` function |
| 69 | + with the following code to retrieve results that match a |
| 70 | + database query and render it in the view: |
| 71 | + |
| 72 | + .. code-block:: php |
| 73 | + |
| 74 | + public function show() |
| 75 | + { |
| 76 | + return view('browse_movies', [ |
| 77 | + 'movies' => Movie::where('runtime', '<', 60) |
| 78 | + ->where('imdb.rating', '>', 8.5) |
| 79 | + ->orderBy('imdb.rating', 'desc') |
| 80 | + ->take(10) |
| 81 | + ->get() |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + .. step:: Add a Web Route |
| 86 | + |
| 87 | + Navigate to the ``routes`` directory and open the ``web.php`` file. |
| 88 | + Add an import for the ``MovieController`` and a route called |
| 89 | + ``browse_movies`` as shown in the following code: |
| 90 | + |
| 91 | + .. code-block:: php |
| 92 | + |
| 93 | + <?php |
| 94 | + |
| 95 | + // ... |
| 96 | + use App\Http\Controllers\MovieController; |
| 97 | + |
| 98 | + Route::get('/browse_movies/', [MovieController::class, 'show']); |
| 99 | + |
| 100 | + .. step:: Generate a View |
| 101 | + |
| 102 | + Navigate to the application root directory and run the following |
| 103 | + command to create a view that displays movie data: |
| 104 | + |
| 105 | + .. code-block:: bash |
| 106 | + |
| 107 | + php artisan make:view browse_movies |
| 108 | + |
| 109 | + .. code-block:: none |
| 110 | + :copyable: false |
| 111 | + |
| 112 | + INFO View [resources/views/browse_movie.blade.php] created successfully. |
| 113 | + |
| 114 | + Navigate to the ``resources/views`` directory and open the |
| 115 | + ``browse_movie.blade.php`` file. Replace the contents with the |
| 116 | + following code: |
| 117 | + |
| 118 | + .. code-block:: bash |
| 119 | + |
| 120 | + <!DOCTYPE html> |
| 121 | + <html> |
| 122 | + <head> |
| 123 | + <title>Browse Movies</title> |
| 124 | + </head> |
| 125 | + <body> |
| 126 | + <h2>Movies</h2> |
| 127 | + |
| 128 | + @forelse ($movies as $movie) |
| 129 | + <p> |
| 130 | + Title: {{ $movie->title }}<br> |
| 131 | + Year: {{ $movie->year }}<br> |
| 132 | + Runtime: {{ $movie->runtime }}<br> |
| 133 | + IMDB Rating: {{ $movie->imdb['rating'] }}<br> |
| 134 | + IMDB Votes: {{ $movie->imdb['votes'] }}<br> |
| 135 | + Plot: {{ $movie->plot }}<br> |
| 136 | + </p> |
| 137 | + @empty |
| 138 | + <p>No results</p> |
| 139 | + @endforelse |
| 140 | + |
| 141 | + </body> |
| 142 | + </html> |
| 143 | + |
| 144 | + .. step:: Start your Laravel Application |
| 145 | + |
| 146 | + Navigate to the application root directory and run the following command |
| 147 | + to start your PHP built-in web server: |
| 148 | + |
| 149 | + .. code-block:: bash |
| 150 | + |
| 151 | + php artisan serve |
| 152 | + |
| 153 | + If the server starts successfully, you should see the following message: |
| 154 | + |
| 155 | + .. code-block: none |
| 156 | + |
| 157 | + INFO Server running on [http://127.0.0.1:8000]. |
| 158 | + |
| 159 | + Press Ctrl+C to stop the server |
| 160 | + |
| 161 | +.. step:: View the Movie Data |
| 162 | + |
| 163 | + Open http://127.0.0.1:8000/browse_movies in your web browser. If it runs |
| 164 | + successuflly, you should see a list of movies and details about each of them. |
| 165 | + |
| 166 | + .. tip:: |
| 167 | + |
| 168 | + You can run the ``php artisan route:list`` command from your application |
| 169 | + root directory to view a list of available routes. |
| 170 | + |
| 171 | +.. include:: /includes/quick-start/troubleshoot.rst |
| 172 | + |
| 173 | +.. button:: Write Data |
| 174 | + :uri: /quick-start/write-data/ |
0 commit comments