Way To Check Laravel Application Running Environment

Hello artisan,

In this check whether the app is in a specific environment in laravel tutorial, you will learn that how to check a laravel application is running on production or local. So laravel check if in production is the today's tutorial.

You know that sometimes you’d come across a scenario where you’d like to check the application environment and based on that you’d like to render things. I am here to solve your problem. We can do it in Laravel in many ways.

See the example code:

if (App::environment(['local', 'staging'])) {
    //code goes here 
}

// or 

if (app()->environment(['local', 'staging'])) {
    //code goes here
}

 

If you want to check it in blade file, you need to do that. 

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

 

Laravel also provided some in-built Blade directives to check for the application environment condition. Here’s how the previous code would translate if we use Laravel’s in-built Blade directive

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

 

If you want to check for multiple environments in a single conditional statement, you can do it like so.

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

 

Read also: Load Your Public Assets With Versioning in Laravel

 

Hope it can help you.

 

#laravel #laravel-8x #laravel-tips