Laravel 9 How To Return JSON Response Example Tutorial

Reading Time: 2 minutes
3,940 Views

Inside this article we will see the concept i.e Laravel 9 How To Return JSON Response tutorial. Article contains classified information about returning json response from laravel application. This concept is too much useful when you are building laravel apis and returning json response.

If you are looking for an article which gives you the understanding to return a json response in laravel application then this article will help you a lot for this.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Laravel JSON Response

Open any controller from your laravel application.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class JsonController extends Controller
{
    /**
     * Json Response a new controller instance.
     *
     * @return void
     */
    public function getJsonData()
    {
        $myArray = [
            "id" => 3,
            "name" => "Sanjay Kumar",
            "email" => "sanjay_test@gmail.com"
        ];
      
        return response()->json($myArray);
    }
}

Concept

Here, is the concept you can use to return JSON response from laravel controller.

$myArray = [
    "id" => 3,
    "name" => "Sanjay Kumar",
    "email" => "sanjay_test@gmail.com"
];

return response()->json($myArray);

We hope this article helped you to Laravel 9 How To Return JSON Response Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.