Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to use environment variables in your laravel project, here we share examples of using env variable in controller and blade.

Laravel provide env() helper function to access env variable in laravel. You have to pass only veriable name in env() function like env('VARIABLE_NAME');.

Now we know the syntax of using env() in laravel.


Let us understand for example using in laravel controller and blade.

ENV Laravel controller example

Here, we use APP_ENV variable to check App environment local or production in laravel controller.

if (env('APP_ENV') == 'local'){
    echo 'Local Enviroment';

ENV Laravel blade example

In Laravel Blade calling env syntax is same as in the controller but different is using a variable in if... else statement.

@if (env('APP_ENV') == 'local')
   Local Enviroment

using env() function you are not getting value or getting wrong value to try to clear your configuration using the below command.

$ php artisan config:clear
Configuration cache cleared!

Thanks For Reading…