Laravel 9 How To Return JSON Response Example Tutorial

Share this Article
Reading Time: 2 minutes
291 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5