Laravel 5.6 .env value resolving as (unknown) - laravel

I have these values in my .env file:
APP_IMG_PATH="/img/"
APP_IMG_LOGO="sclogo.png"
Then in my header I do this:
<img src="{!! env('APP_IMG_PATH') . env('APP_IMG_LOGO') !!}" class="navbar-logo"/>
But the log does not show and in Chrome Inspect I get
<img src(unknown) class="navbar-logo">
I tried clearing config cache and recreating it:
php artisan config:cache;
But the result is the same. Any help is appreciated.

Config caching disables env() calls. Any call to env() will return null once the config is cached.
Perhaps a poor choice by the Laravel team, but the idea is to encourage you to use config(), not env() within your code.
Instead, add new lines to your config/app.php (if that's where you choose):
'img_path' => env('APP_IMG_PATH'),
'img_logo' => env('APP_IMG_LOGO'),
Then re-create your config cache and use config('app.img_path') and config('app.img_logo') within your application.

After addig new things to env file, you need to clear your cache.
php artisan config:clear
php artisan config:cache

Related

How can I use .env.example constant in HTML view?

In my .env.example file, I have defined a constant, e.g. BUSINESS_ID=1234
I would like to use this constant in my test.blade.php file like so:
<p>{{ env.example(BUSINESS_ID) }}</p>
But Laravel doesn't like this formatting - please advise as to how I can call the constant from the .env.example file to appear on the front-end. Thanks.
Don’t confuse it with .env.example file – it is used as an information file for all team members to know what keys and values may be needed & .env.example is never loaded. See The Official Documentation
If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.
The .env file is your environment variables. So you’re giving your Laravel app the information which is relevant to its local environment.
According to above what you need to do is as below :
{{ env('BUSINESS_ID') }} // returns 1234
{{ env('APP_URL') }} // returns App Url
Then from console you need to clear cache.
php artisan config:clear
php artisan cache:clear

Laravel after upgrade, .env variable Undefined index

I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Try using the env helper instead, env('API_DOMAIN')
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env() helper outside of config files, because when you cache your config, env() will return null.

Laravel - accessing .env variables

I tried to get the environment variable from the .env in my root with
Route::get('/test', function () {
return "value is". getenv('APP_ENV');
});
and
Route::get('/test', function () {
return "it is". env('APP_ENV');
});
It is in .env
APP_NAME=Laravel
APP_ENV=local
How can I get access to it?
With Laravel, you should avoid environmental variables outside of your configuration files.
In your config files, you can use environmental variables, example in config/app.php:
'env' => env('APP_ENV', 'production'),
Then you can access this using the config helper: config('app.env').
This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached.
Route::get('/test', function () {
return "it is".config('app.name');
});
use env('ENVKEY')
Don't forget to clear cache sometime it cause because of cache.
php artisan config:clear
php artisan cache:clear
composer dump-autoload
For more info look at the doc
just run this commands in cmd.
php artisan config:cache
then
php artisan config:clear
then
php artisan cache:clear
As per the Laravel Documentation on Environment Configuration,
All of the variables listed in this file will be loaded into the $_ENV
PHP super-global when your application receives a request. You may use
the env helper to retrieve values from these variables.
So, it is possible to access the variable as
$_ENV['envKey'];
laravel provides a global helper function for this kind of task
$val = config('app.something');
you can also set new values with the following method
config(['app.something' => 'cat']);
reference
for your particular task it would be
$val = config('app.env');
or to determine the env globally
$environment = App::environment();
i hope this helps, have a nice one!
App::environment()
try this please

Laravel can't change config value

I found the problem, i'll try to use a homestead or docker.
if i restart php artisan serve the changed values are applied.
Same my post: Laravel can't change config value
I make route
Route::get('/config', function () {
return dd(config('app'));
});
And cant change my config/app.php, for example i want to change
'url' => env('APP_URL', 'test'),
'client_url' => env('APP_CLIENT_URL', 'test'),
I set
APP_CLIENT_URL=str1
APP_URL=str2
and there is something inexplicable:
My "url" => "http://127.0.0.1:8000" !!! not default 'test' or from .env, I did not set such a value.
My client_url contains the old value, after I cached the config.
This is not production, i accidentally run config:cache, but after i remove all cache from bootsrap/cache, storage/framework/cache and running cache:clear, config:clear.
After running config:cache values from the .env were put in config, but I do not want to constantly change cache in development and it is not recommended in the documentation.
How can I easily change .env in in developing?

Laravel 5.6 env('APP_BASE_URL') returns null

In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.
APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com
APP_BASE_URL="https://app-staging.app.com"
Clearing Cache
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
But when using following in blade view gets nothing
env('APP_BASE_URL') returns null
It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.
See here: https://laravel.com/docs/5.6/configuration#configuration-caching
This post solved my issue, I defined custom config in config/app.php
https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable
/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
*/
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Then in .env I definded:
APP_BASE_URL="https://app-staging.app.com"
Finally cleared the cache, worked.
In blade view
{{ config('app.base_url') }}
Yes it will return only null
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.
See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching

Resources