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

Uncategorized Laravel database transactions

How to use database transaction in laravel eloquent with example ?

January 1, 2022February 22, 2024

Database Laravel Transaction in laravel eloquent are used to execute multiple or single database operations which maintainer ACID(atomicity, consistency, isolation, and durability) property of transactions. In laravel we can archive the database transaction by DB facade. Laravel DB facade provides two methods to handle the transactions one through DB::transaction() method…

Read More

What is httpd.conf and httpd-vhost.conf file in apache?

August 28, 2021August 28, 2021

https.conf is main file of apache web server to handle the requests. Apache httpd.conf file generally located at /etc/httpd/conf/httpd, /etc/apache2/ , /etc/apache2/sites-enabled/ in ubuntu. httpd.conf contains the information of server root and port used routing. httpd-vhost contains additional information of virtual host.

Read More
Uncategorized update page data without reload

How to update page data without reload in jQuery?

October 24, 2021November 17, 2023

If you are beginner then this post is for you. For showing the new content on page without refresh we will use JQuery Reload Page without Refresh and server side PHP. In this article, I will demonstrate to show the latest content from server without refreshing the page. This is…

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