Laravel Check App Environment Running in Production or Development Mode

Sovary October 24, 2022 748
3 minutes read

In Laravel, we have options to mark as our project is in development mode or production mode which is easy to stack trace errors or what else we can do. In developments mode we don't want to show something detail in public, and this we can do in Laravel project to check whether we want to show in public or in local developments mode.

This tutorial today, we will explain and show examples how to check running app environment. If you are looking for solution to check your environment app is running and this tutorial we will provide example with solution in controller file and blade view as well. We will go into detail how to check the current environment running in Laravel app. Within this example below you can use with versions Laravel 6, Laravel 7, Laravel 8, and Laravel 9.

Before go to example code, we would introduce you a file in Laravel root directory which called .env file which help you to enable the mode. You may see a configure name APP_ENV=local or APP_ENV=production . These setting will help you to switch between production mode and local mode. You have to modified the setting to preferred mode after implement below code.

All the last step, you have to clear configure with artisan command after changed .env file

php artisan config:clear

So let's see below examples to implement how to check app running environment in Laravel project.

Example #1 - Using Facade in Controller File

We can use if-else statement to execute which block of statements in code to run in production mode.

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App;
  
class ArticleController extends Controller
{

    public function index()
    {
        if (App::environment(['production'])) 
        {
            dd("In production");
        }
  
    }
}

Example #2 - Using Helper in Controller File

Another example using facade method to check environment to run block of statements.

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ArticleController extends Controller
{

    public function index()
    {
        if (app()->environment(['local', 'staging']))
        {
            dd("In local or staging.");
        }
  
    }
}

Example #3 - Using Facade in Blade File

We also can check environment in blade file with helper. Example below the block of statements will execute when you configure as in production mode.

<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
   @if(App::environment('production'))
    <!-- You code to run in production -->
   @endif
</body>
</html>

Example #4 - Using Directive in Blade File

Another option you can use directive @production to open and end the block of production to be execute.

<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
   @production
      <!--You code here -->
   @endproduction
</body>
</html>

Example #5

For this option which you can check in production or staging mode by pass the value into parenthesis.

<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
  @env('local', 'staging')
    <!-- You code Here -->
  @endenv
</body>
</html>

Hope this article will help you to check app running in development or production mode. Have a nice day!!

You may also like...

Laravel  PHP  Laravel 9 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him