How to Check Laravel App Environment?

Spread the love

Hello Guys,

Are you curious about checking the environment of your Laravel application? In this tutorial, we will guide you through the process of verifying your app’s environment step by step. We’ll provide straightforward examples that you can follow. If you’re looking to see an example of a Laravel production environment, you’re in the right place.

These techniques are compatible with various Laravel versions, including Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10.

Determining whether your Laravel application is running in a production or staging environment is essential. Fortunately, there are several methods to achieve this. We will explore four different approaches to check the app’s current environment:

  1. App::environment()
  2. app()->environment()
  3. @production
  4. @env

Let’s dive into each of these examples one by one to ensure you can effectively verify your Laravel app’s environment.

Check Laravel App Environment :

Also Read: How to use Ajax Autocomplete Search using JQUERY UI in laravel 10

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:

Also Read: How to upload Images using LARAVEL 10

<?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.");
        }
  
    }
}

Example 3:

Also Read: how to use Cron Job Task Scheduling using LARAVEL 10

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

Example 4:

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

Example 5:

Also Read: How to install Docker on Windows 10/11 STEP BY STEP

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

Discover the best in Laravel blog through our thoughtfully crafted content.

Leave a Comment