In this artical, i will show you how to get config variable (env variable) in view file laravel. we can get env variable value in controller and blade file too. we can simple print env file variable by using env() helper.
In tthe following example, we will add a new envirnment variable. i create “GOOGLE_URL” variable in .env file and i will access to this variable in blade or controller by using env() helper function.
Syntax :
1 2 3 | env('VARIABLE_NAME'); |
add this line in .env file
1 2 3 | GOOGLE_URL=www.google.com |
to access the value of a variable in Blade file
1 2 3 | {{ env('GOOGLE_URL') }} |
use variable in if statement
1 2 3 4 5 | @if ( !empty(env('GOOGLE_URL')) ) we are in :p @endif |
to access the value of a variable in Controller file
1 2 3 | env('GOOGLE_URL'); |
use variable in if statement
1 2 3 4 5 | if ( !empty(env('GOOGLE_URL')) ){ echo 'google url -> '.env('GOOGLE_URL'); } |
If your config is cached, you’d have to regenerate it if you change something. Try
1 2 3 | php artisan config:clear |