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 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 dependency injection angular

What is Dependency Injection in Angular?

June 20, 2021November 8, 2023

In a simple term we can say dependency injection in angular are created once at the time of initialisation of service or component then throughout the application we can reuse same object without re-initialisation. So, this is clear now advantage of dependency injection is to not creating the object in…

Read More
Uncategorized setup simple jQuery form validation

How to setup simple jQuery form validation?

September 15, 2021September 29, 2021

In this tutorial, we are going to learn jQuery form validation using jQuery validation plugin. this plugin have many features like to validate text, email, phone number , sync Ajax request etc. Let’s begin with step by step guide: Step 1: Create a html file Firstly we are going to…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version