How to Check Running Laravel App Environment?

Hello dev, Today we are going to learn How to Check Running Laravel App Environment? This tutorial will cover on check laravel app environment running or not!

In this tutorial, we will go over the demonstration of laravel check app environment. step by step explain how to check app environment in laravel. This article will give you a simple example of check laravel app running environment. if you want to see an example of laravel production env environment then you are in the right place.

You can use this tips in laravel 6, laravel 7, laravel 8 and laravel 9 versions.

If you want to check your laravel application running in which environment like staging or production. Then there are several ways to do this. we will use App::environment()app()->environment(), @production and @env to check app current env. so let’s check one by one example as the below:

Also Read: How to Check If Request Has File in Laravel?

Examples on How to Check Running Laravel App Environment

Example 1:

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        if (App::environment(['local', 'staging'])) {
            dd("This is Local or Staging App");
        }
  
    }
}

Example 2:

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        if (app()->environment(['production'])) {
            dd("This is production app.");
        }
  
    }
}

Also Read: Laravel Blade Check if Array Key Exists Example

Example 3:

@if(App::environment('production'))
    {{-- in "production" environment --}}
@endif

Example 4:

@production
    {{-- in "production" environment --}}
@endproduction

Example 5:

@env('local', 'staging')
    {{-- in "local" or "staging" environment --}}
@endenv

Conclusion

Today, We had learn How to Check Running Laravel App Environment? Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel Blade Check If Variable Exists or Not with Example

1 Comment

Leave a Reply