3 Methods to Check if Debug Mode is Enabled in Laravel 9

3 Methods to Check if Debug Mode is Enabled in Laravel 9

Laravel framework has a debug mode that helps to debug application during development. Debug mode determines what information can be displayed to the user when error occurs.

This tutorial provides 3 methods how to check if debug mode is enabled in Laravel 9 application.

Debug mode can be enabled or disabled by using APP_DEBUG environment variable in .env file.

.env

APP_DEBUG=true

Don't forget to clear configuration cache if .env has been changed.

php artisan config:cache

Method 1 - 'App' facade

The App facade provides the hasDebugModeEnabled method to determine if debug mode is enabled.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;

class TestController extends Controller
{
    public function index(): Response
    {
        $debug = App::hasDebugModeEnabled();

        return new Response($debug ? 'Enabled' : 'Disabled');
    }
}

Method 2 - 'app' helper function

The app helper function allows to get an instance of Application class. It provides the hasDebugModeEnabled method to determine if debug mode is enabled.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;

class TestController extends Controller
{
    public function index(): Response
    {
        $debug = app()->hasDebugModeEnabled();

        return new Response($debug ? 'Enabled' : 'Disabled');
    }
}

Method 3 - 'config' helper function

The config helper function can be used to determine if debug mode is enabled.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;

class TestController extends Controller
{
    public function index(): Response
    {
        $debug = config('app.debug');

        return new Response($debug ? 'Enabled' : 'Disabled');
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.