php-laravelHow do I use the env() function in Laravel with PHP?
The env()
function in Laravel is used to retrieve environment variables from the .env
file. It is a wrapper around the PHP getenv()
function, which retrieves variables from the system environment.
Example
$database_name = env('DB_DATABASE');
echo $database_name;
Output example
my_database
The env()
function takes two parameters, the first being the name of the environment variable, and the second being an optional default value to return if the environment variable does not exist.
env('DB_DATABASE')
- retrieves the value of theDB_DATABASE
environment variable from the.env
file.env('DB_DATABASE', 'default_value')
- retrieves the value of theDB_DATABASE
environment variable from the.env
file, or returnsdefault_value
if the variable does not exist.
Helpful links
More of Php Laravel
- How can I use the @yield directive in PHP Laravel?
- How do I set up a Laravel worker using PHP?
- How do I set the timezone in PHP Laravel?
- How do I fix an undefined variable error in PHP Laravel?
- How do I generate a UUID using Laravel and PHP?
- How do I use Redis with Laravel in PHP?
- How do I quickly get started with Laravel using PHP?
- How can I generate ideas for a PHP Laravel project?
- How do I use Laravel Valet with PHP?
- How can I use the "order by" function in PHP Laravel?
See more codes...