How To Get env Variable inside Laravel Blade Templates

Reading Time: 3 minutes
200 Views

Laravel, a popular PHP framework, provides a versatile and powerful environment for developing online applications. While configuration values and environment variables are frequently defined in Laravel’s.env file, you may need to access these data directly within your Blade templates.

This tutorial will walk you through accessing environment variable values within Laravel Blade templates, allowing you to dynamically customise your views based on the settings of your application.

Read More: Laravel 10 How To Update Data by Ajax Request Tutorial

We’ll look at ways and provided methods for accessing environment variables within Blade templates throughout this lesson.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

What are env Variable in laravel?

The env function and.env files in Laravel are used to handle environment variables, which are often known as configuration variables or just “env variables.” These variables store sensitive or environment-specific Laravel application setup settings.

Using environment variables is a suggested practise for separating setup from code and keeping sensitive data secure.

Example

An example of a .env file might look like this:

APP_NAME="Online Web Tutor"
APP_ENV=production
APP_KEY=base64:YOUR_SECRET_KEY_HERE
APP_DEBUG=false
...
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=my_username
DB_PASSWORD=my_password

Now, let’s see how to fetch the value of APP_NAME in blade views.

Example #1: Using env() Helper Function

Inside blade template you can use env() helper function to get access of env values.

<h3>{{ env('APP_NAME') }}</h3>

Output

Online Web Tutor

Read More: Laravel 10 Re-Usable Component Class Example Tutorial

Also, you can use env variable values inside laravel directives,

@if(env('APP_ENV') == 'local')

       Match

@endif

Additionally, you can use the env values inside script tags,

<script>

      var name = "{{ env('APP_NAME') }}";

      console.log(name);

</script>

Example #2: Using config() Helper Function

Also you can use config() helper function of laravel,

{{ config('app.name') }}

app.name means APP_NAME.

For LOG_CHANNEL, it will be like log.channel

That’s it.

We hope this article helped you to learn How To Get env Variable inside Laravel Blade Templates in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more