Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel 8 modify JSON response

How to modify JSON response in Laravel 8?

Aman Jain, October 15, 2021January 26, 2022

In this tutorial we will learn to modify the JSON and add new content to it. sometime we need to add some global data to every API, so in this article we will create a middleware and temper the sons response.

I assume you well know to Laravel middleware and if not then you can read this article custom Laravel middlware

Let’s begin with simple step by step process:

Step 1: Create a middleware

Create a middleware using simple artisan command

php artisan make:middleware JsonResponseMiddleware

Step 2: Add Middleware in routes in Laravel 8

Create routes and use the middleware

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\Api\PostController;
use App\Http\Middleware\JsonResponseMiddleware;
 
Route::middleware(JsonResponseMiddleware::class)->group(function(){
  Route::get("add-post",[PostController::class, 'create']);
});

Step 3: Implement the logic in middleware

We have created our custom middleware and defined in kernel, now it’s time e to implement the our core logic for check blacklisted user.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class JsonResponseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);  // after request completion get the controller response her
        
        if($response instanceof \Illuminate\Http\JsonResponse){

            $current_data = $response->getData();

            $current_data->user_status = ['add some data here'];

            $response->setData($current_data);
        }
        return $response;
        
    }
}

As you can see we have handled $response = $next($request); the response and checking here that if $response variable is instance of \Illuminate\Http\JsonResponse then we can add others keys to response data.

Step 4: Create Controller and test the implementation

Now, create a controller in api folder and check the implementation

<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class PostController extends Controller
{

     /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
       return response()->json(["status"=>true,"message"=>"success"]);
    }
}

Output:

Screenshot 2021 10 15 at 2.32.04 PM

Related

Uncategorized

Post navigation

Previous post
Next post

Related Posts

Laravel Create database connection in laravel

How to make database connection in Laravel 8 ?

December 14, 2021February 22, 2024

Laravel comes with first party support for several database drivers. Laravel gives much better flexibility in terms of using the RDBMS based databases. Laravel supports MySQL, PostgreSQL, SQLite and SQL Server by default but if you want to make connection with Mongo or other database then you need to install…

Read More
Uncategorized confirm password validation in Laravel

Password and confirm password validation in Laravel

January 26, 2022November 10, 2023

There is many ways to check the password and confirm password validation in laravel. we can match password using the equals to operator but in that case we need to work as core PHP. So In laravel we have Validation library to create the validation rules. Laravel itself provides multiple…

Read More
Uncategorized call api in angular

How to call api in angular 12?

September 10, 2021November 7, 2023

In this tutorial, we are going to call the rest API’s using the angular HttpClient. To fetch data from server we need to call api in angular so we can get the updated data from serve. Angular has vast features and library to call the web or rest API’s. Step…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version