For my situation, I need to append debugbar infomation into APIs response when JSON API respond, here is my solution.

You can make other sevice provider for this, but I just want to make a concept for you, so put in AppServiceProvider.

What’s position that I want to put on, see in below:

class Kernel extends HttpKernel
{
    protected $middleware = [

        // Here is prepend position.

        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

        // Here is push position.
    ];

Next, we need to create a after middleware and setData into request:

class ProfileJsonResponse
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        if ($response instanceof JsonResponse && is_object($response->getData())) {
            $response->setData($response->getData(true) + [
                    '_debugbar' => app('debugbar')->getData(),
                ]);
        }

        return $response;
    }
}

And finally I added some condition to push middleware into request pipeline:

//service.provider

public function boot()
{
    if (env('APP_DEBUG') && $this->app->isLocal()) {
        $kernel = $this->app[Kernel::class];

        if ($this->app->bound('debugbar') && $this->app->make('debugbar')->isEnabled() && ! $this->app->runningUnitTests()) {
            $kernel->pushMiddleware(ProfileJsonResponse::class);
        }
    }
}