Reputation: 692
i have worked on laravel with quite some time now with laravel 5.0 to 5.8 i am currently using laravel 5.7 in a project , i am declaring some constants in my env file but i am unable to print them in my controller here is constants in my env file
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:Qm/Xwz4V9oJLhlk8RiY4usy4cdruFHRTe8iYkj7suMs=
APP_DEBUG=true
APP_URL=http://devmarketplace.finpay.pk
LOG_CHANNEL=stack
DB_CONNECTION=mysql
#crm database
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=marketplacecrm
DB_USERNAME=root
DB_PASSWORD=
#marketplace database
LIVE_READ_DB_HOST=localhost
LIVE_WRITE_DB_HOST=localhost
#marketplace database
LIVE_DB_DATABASE=marketplace
LIVE_DB_USERNAME=root
LIVE_DB_PASSWORD=
#marketplace logs
LIVE_LOGS_DB_HOST=localhost
LIVE_LOGS_DB_DATABASE=marketplacelogs
LIVE_LOGS_DB_USERNAME=root
LIVE_LOGS_DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
PAYROLL_BANKS=http://devpayrollplus.finpay.pk/public/apiV2/
UNEEB=UNEEB
now i am accessing them in my controller like
public function index(){
echo env("UNEEB");
exit();
return view("stores.stores");
}
i have performed a number of steps after adding value in my env
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan config:cache
but it still doesnt work, the funny part is i have another project on laravel 5.8 and it works fine over their, here is my composer.json and package.json in case something went wrong over their.
composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.1.3",
"awobaz/compoships": "^1.1",
"aws/aws-sdk-php-laravel": "^3.3",
"barryvdh/laravel-dompdf": "^0.8.3",
"donatello-za/rake-php-plus": "^1.0",
"fideloper/proxy": "^4.0",
"genealabs/laravel-model-caching": "^0.3.6",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "5.7.*",
"laravel/telescope": "^1.0",
"laravel/tinker": "^1.0",
"maatwebsite/excel": "^3.1",
"spatie/laravel-activitylog": "^3.1",
"spatie/laravel-database-mail-templates": "^2.0",
"spatie/laravel-fractal": "^5.4",
"spatie/laravel-permission": "^2.29"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.2",
"beyondcode/laravel-dump-server": "^1.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"files": [
"app/Helpers/GeneralHelper.php"
],
"psr-4": {
"App\\": "app/",
"Model\\": "app/Models",
"Report\\": "app/Reports",
"Settlement\\": "app/Settlements",
"Batch\\": "app/Batches"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
Package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.18",
"cross-env": "^5.1",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.5",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue-template-compiler": "^2.5.21",
"webpack-cli": "^3.3.5"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.6.3",
"bootstrap": "^4.2.1",
"bootstrap-daterangepicker": "^3.0.3",
"bootstrap-vue": "^2.0.0-rc.11",
"css-loader": "^2.1.0",
"datatables.net": "^1.10.19",
"datatables.net-autofill-bs4": "^2.3.2",
"datatables.net-bs": "^1.10.19",
"datatables.net-bs4": "^1.10.19",
"datatables.net-buttons-bs4": "^1.5.4",
"datatables.net-colreorder-bs4": "^1.5.1",
"datatables.net-fixedcolumns-bs4": "^3.2.6",
"datatables.net-fixedheader-bs4": "^3.1.5",
"datatables.net-keytable-bs4": "^2.5.0",
"datatables.net-responsive-bs4": "^2.2.3",
"datatables.net-rowgroup-bs4": "^1.1.0",
"datatables.net-rowreorder-bs4": "^1.2.5",
"datatables.net-scroller-bs4": "^1.5.1",
"datatables.net-select-bs4": "^1.2.7",
"fs": "0.0.1-security",
"jquery": "^3.3.1",
"jshint": "^2.10.2",
"mdbootstrap": "^4.7.0",
"moment": "^2.24.0",
"nprogress": "^0.2.0",
"popper.js": "^1.14.6",
"vue": "^2.5.22",
"vue-fullscreen": "^2.1.3",
"vue-sweetalert2": "^2.1.0",
"vue2-daterange-picker": "^0.1.6",
"vuejs-datepicker": "^1.5.4"
}
}
Upvotes: 0
Views: 794
Reputation: 64
You are calling
echo env("UNEEB")
while it is declared with 3 capital E's in your env file.
Upvotes: 0
Reputation: 3572
Make sure you have stopped the server once after new change in .env
if you are using php artisan serve
to load new changes.
Because default it run the configuration given at that time the server start.
So for loading each new changes from .env
you must have to kill the process and re-run php artisan serve
Edited:
Also confirm, If there is any variable in .env
contains spaces or special charectors
make sure you have wrap them in double quotes
.
Upvotes: 2
Reputation: 432
Add your constant to .env like
[email protected]
Add the same to your config/app.php
'admin_email' => env('ADMIN_EMAIL', '[email protected]'),
Retrieve your value
{{config('app.admin_email')}}
Upvotes: 2
Reputation: 131
Strange, those commands that you performed should've fixed your issue... Anyways, Laravel it's a bit sensitive with its .env file, so for your usage, I would change the approach of constants.
Laravel recommends only to use env()
within the configuration files. Try use the config()
helper in your code instead of env()
. Move your constants into a new file and then you can call config('blah-blah-name')
in your code.
Hope it will help.
LE: If you using php artisan serve
, it might be the cause of your problem.
Upvotes: 0