From d1802d1664d2c61bf3c9ec1ce9960728a5b2db50 Mon Sep 17 00:00:00 2001 From: Nora Reidy Date: Mon, 6 May 2024 10:49:47 -0400 Subject: [PATCH] DOCSP-37770: Return results as JSON (#2921) Adds an optional quick start task to return results as JSON --- docs/query-builder.txt | 5 ++++- docs/quick-start/view-data.txt | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/query-builder.txt b/docs/query-builder.txt index 9547b1676..8b4be3245 100644 --- a/docs/query-builder.txt +++ b/docs/query-builder.txt @@ -73,7 +73,10 @@ Before You Get Started To run the code examples in this guide, complete the :ref:`Quick Start ` tutorial to configure a web application, load sample datasets into your MongoDB deployment, and -run the example code from a controller method. +run the example code from a controller method. To see the expected code +output as JSON documents, use the ``toJson()`` method shown in the optional +:ref:`View your results as JSON documents ` step +of the Quick Start. To perform read and write operations by using the query builder, import the ``Illuminate\Support\Facades\DB`` facade and compose your query. diff --git a/docs/quick-start/view-data.txt b/docs/quick-start/view-data.txt index 6ce31369e..86860944e 100644 --- a/docs/quick-start/view-data.txt +++ b/docs/quick-start/view-data.txt @@ -136,6 +136,29 @@ View MongoDB Data + + .. _laravel-quick-start-json: + + .. step:: 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: + + .. code-block:: php + + public function show() + { + $results = Movie::where('runtime', '<', 60) + ->where('imdb.rating', '>', 8.5) + ->orderBy('imdb.rating', 'desc') + ->take(10) + ->get(); + + return $results->toJson(); + } .. step:: Start your Laravel application