Q:

Laravel 6 - Conditionally enable debug page on production mode

I used to run debug true in production when needed with Laravel 5 the following way: 
'debug' => env('APP_DEBUG', $_SERVER['REMOTE_ADDR'] == 'myipaddress' ? true : false),

However Laravel 6 doesn't let me use it, when I do artisan config:cache, artisan complains that:

variable $_server['REMOTE_ADDR'] is not defined and exists.

Is there another way someone has found out to be working to do this with Laravel 6?

A:

You can't cache dynamic configs. there is no request and no $_server when Laravel tries to cache your configs.
You must disable your debug on production (APP_DEBUG = false) and check the log for any errors.
But if you insist to enable app debug dynamically, you can use middleware:

Create a new middleware using Artisan command:
php artisan make:middleware EnableDebug

This command will place a new EnableDebug class within your app/Http/Middleware directory. Modify it like this:
<?php

namespace App\Http\Middleware;

use Closure;

class EnableDebug
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        config(['app.debug' => $request->ip() === 'myipaddress']);

        return $next($request);
    }
}

List your middleware class at the end of the $middleware property of your app/Http/Kernel.php class:
protected $middleware = [
    //...
    \App\Http\Middleware\EnableDebug::class,
];

