File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,10 @@ Before You Get Started
7373To run the code examples in this guide, complete the
7474:ref:`Quick Start <laravel-quick-start>` tutorial to configure a web
7575application, load sample datasets into your MongoDB deployment, and
76- run the example code from a controller method.
76+ run the example code from a controller method. To see the expected code
77+ output as JSON documents, use the ``toJson()`` method shown in the optional
78+ :ref:`View your results as JSON documents <laravel-quick-start-json>` step
79+ of the Quick Start.
7780
7881To perform read and write operations by using the query builder, import the
7982``Illuminate\Support\Facades\DB`` facade and compose your query.
Original file line number Diff line number Diff line change @@ -136,6 +136,29 @@ View MongoDB Data
136136
137137 </body>
138138 </html>
139+
140+ .. _laravel-quick-start-json:
141+
142+ .. step:: Optionally, view your results as JSON documents
143+
144+ Rather than generating a view and editing the ``browse_movie.blade.php`` file, you can
145+ use the ``toJson()`` method to display your results in JSON format.
146+
147+ Replace the ``show()`` function with the following code to retrieve results and
148+ return them as JSON documents:
149+
150+ .. code-block:: php
151+
152+ public function show()
153+ {
154+ $results = Movie::where('runtime', '<', 60)
155+ ->where('imdb.rating', '>', 8.5)
156+ ->orderBy('imdb.rating', 'desc')
157+ ->take(10)
158+ ->get();
159+
160+ return $results->toJson();
161+ }
139162
140163 .. step:: Start your Laravel application
141164
You can’t perform that action at this time.
0 commit comments