In this article
Introduction
I was recently browsing through X and saw an awesome code tip from Oussama Mater about a method I didn't know existed in Laravel. So I wanted to put together a quick article to share it with you all so you can benefit from it too!
As a side note, if you're not already following Oussama (@OussamaMater) on X, I highly recommend you do so. He shares some great tips and tricks for Laravel developers.
The tip is about using Laravel's Env::getOrFail
method to ensure an environment variable exists when reading it in your config files:
Laravel Tipπ‘: Ensure Env Keys Exist
— OussamaMater (@OussamaMater) August 10, 2024
If you want to be absolutely sure that a key exists in your .env file, use the "getOrFail()" method. It will throw a runtime exception if the key is missing. This is really useful for API keys π#laravel pic.twitter.com/V4mSza6Spd
Using the Env::getOrFail
Method
The Env::getOrFail
method was contributed back in September 2023 by Lucas Michot in PR #48261 and merged into Laravel 10. Thanks for the awesome contribution, Lucas!
Let's take a look at an example of using this method in your Laravel applications.
Imagine you want to send emails from your Laravel app using Mailgun. As part of the setup, you'd need to add your Mailgun domain and secret to your .env
file. You might typically do this like by defining the following keys in your config/services.php
file:
config/services.php
1return [23 // ...45 'mailgun' => [6 'domain' => env('MAILGUN_DOMAIN'),7 'secret' => env('MAILGUN_SECRET'),8 'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),9 'scheme' => 'https',10 ],11];
As we can see, we're attempting to read the MAILGUN_DOMAIN
, MAILGUN_SECRET
, and MAILGUN_ENDPOINT
environment variables. If these keys are missing from the .env
file, null
will be returned for these values. Eventually, this will cause an error when we attempt to send the email. But we'd prefer to catch this error as early as possible in the request lifecycle.
We can use the Env::getOrFail
method to ensure these keys exist in the .env
file. If the environment variable is missing, a \RuntimeException
will be thrown. This way, we can catch the error early and fix it before it causes any issues. To use this, we just need to swap out the env
helper with Env::getOrFail
:
config/services.php
1use Illuminate\Support\Env;23return [45 // ...67 'mailgun' => [8 'domain' => Env::getOrFail('MAILGUN_DOMAIN'),9 'secret' => Env::getOrFail('MAILGUN_SECRET'),10 'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),11 'scheme' => 'https',12 ],13];
As we can see in the example, above, we've updated our config/services.php
config file to enforce the existence of the MAILGUN_DOMAIN
and MAILGUN_SECRET
environment variables. If these keys are missing a \RuntimeException
will be thrown with the following message:
1Environment variable [MAILGUN_DOMAIN] has no value.
You may have also noticed that we can still use the env
helper function for the MAILGUN_ENDPOINT
key. This is because we've provided a default value for this key (the Env::getOrFail
method doesn't support default values). If the MAILGUN_ENDPOINT
key is missing from the .env
file, Laravel will use the default value of api.mailgun.net
.
Taking it Further
If you want to take your config validation even further, you may want to check out my Laravel Config Validator package. It allows you to build a set of rules that you can use to validate your Laravel app's config fields. For example, you might want to use it to ensure that an API key is present in the config, is in the correct format, and can also be used to send API requests.
You can check out the article here: How to Validate Your Laravel App's Config
Conclusion
Hopefully, this post has shown you how you can use the Env::getOrFail
method to ensure the existence of environment variables in your Laravel applications. Is this something you'll start using in your own projects?
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! π