Returning JSON response in Laravel

Data transfer is an important task in development. First way to do the task is to use XML. JSON is very easy to read, store arrays and objects with values as strings. JSON accepts these types of data: Double, Float, String, Boolean, Array, Object, Null. It's easy to nest Objects and Arrays in JSON. AJAX is a technology which uses JSON. If you have an AJAX application that hits the front-end, usually the server side will respond with JSON data. This JSON data can be parsed with JavaScript front-end. In PHP there are functions to encode and decode json data. Laravel also makes use of it. Let’s see an example. Using a database query returns it’s output to the browser.


Route::get('/', function()

{

$paintings = Painting::all();

    return $paintings;

 

});


Output

[

 

    {

        "id": 1,

        "title": "Mona Lisa",

        "body": "The Mona Lisa is a half-length portrait of a woman by the Italian artist Leonardo da Vinci, which has been acclaimed as "the best known, the most visited, the most written about, the most sung about, the most 

 

parodied work of art in the world",

        "painter_id": 2,

        "created_at": "2014-01-24 19:49:55",

        "updated_at": "2014-01-24 19:49:55"

    },

    {

        "id": 2,

        "title": "Last Supper",

        "body": "The Last Supper is a late 15th-century mural painting by Leonardo da Vinci in the refectory of the Convent of Santa Maria delle Grazie, Milan. The work is presumed to have been commenced around 1495 and was 

 

commissioned as part of a scheme of renovations to the church and its convent buildings by Leonardos patron Ludovico Sforza, Duke of Milan. ",

        "painter_id": 2,

        "created_at": "2014-01-24 19:55:46",

        "updated_at": "2014-01-24 19:55:46"

    },

    {

        "id": 3,

        "title": "The Starry Night",

        "body": "Starry Night is a painting by the Dutch post-impressionist artist Vincent van Gogh. Painted in June 1889, it depicts the view outside of his sanitarium room window at Saint-Rémy-de-Provence at night",

        "painter_id": 3,

        "created_at": "2014-01-24 19:57:39",

        "updated_at": "2014-01-24 19:57:39"

    },

    {

        "id": 4,

        "title": "The Potato Eaters",

        "body": "The Potato Eaters is a painting by the Dutch painter Vincent van Gogh that he painted in April 1885 while in Nuenen, Netherlands. It is in the Van Gogh Museum in Amsterdam.",

        "painter_id": 3,

        "created_at": "2014-01-24 19:58:31",

        "updated_at": "2014-01-24 19:58:31"

    },

    {

        "id": 5,

        "title": "The Night Watch",

        "body": "The Night Watch or The Shooting Company of Frans Banning Cocq is the common name of one of the most famous works by Dutch painter Rembrandt van Rijn.",

        "painter_id": 4,

        "created_at": "2014-01-24 19:59:44",

        "updated_at": "2014-01-24 19:59:44"

    },

    {

        "id": 6,

        "title": "The Storm on the Sea of Galilee",

        "body": "The Storm on the Sea of Galilee is a painting from 1633 by the Dutch Golden Age painter Rembrandt van Rijn that was in the Isabella Stewart Gardner Museum of Boston, Massachusetts, United States, prior to 

 

being stolen on March 18, 1990.",

        "painter_id": 4,

        "created_at": "2014-01-24 20:00:32",

        "updated_at": "2014-01-24 20:00:32"

    }

 

]


It is easy to nest Objects and Arrays in JSON. Here is an example:


{

   "an_object":{

      "an_array_of_objects":[

         {

            "Art":"Leonardo"

         },

         {

            "Music":"Mozart"

         },

         {

            "Food":"Italian"

         },

         {

            "Fun":"Laravel"

         }

      ]

   }

}


Here is another example.

Ajax response in Laravel controller..


 <!DOCTYPE html>

<html>

<head>

    <title>Laravel Simple Ajax Request</title>

<script src="http://code.jquery.com/jquery-3.3.1.min.js">

</script>

</head>

<body>

<div class="container">

</div>

<script type="text/javascript">

    $.ajax(

    {

        url: "/getAllBodyData",

        type: 'GET',

    }).done( 

        function(data) 

        {

            $('.container').html(data.html);

        }

    );

</script>

</body>

</html>


Laravel controller method.


public function getAllBodyData()

{

    $title = "www.infinityknow.com";

    $view = view("ajaxView",compact('title'))->render();

    return response()->json(['html'=>$view]);

}


ajaxView.blade.php file.


<h1>{{ $title }}</h1>

<p>

<ul>

<li>laravel return view with json data</li>

<li>Handling JSON Data in Laravel 5.7</li>

<li>laravel response json with status code</li>

<li>laravel return ajax response</li>

<li>passing data from ajax to controller laravel</li>

<li>how to retrieve data from database using ajax in laravel</li>

</ul>