How to Get env Variable in Laravel Blade?

Laravel

Nicesnippets

01-08-2022


How to Get env Variable in Laravel Blade?

Hello Friends,

I will explain step by step tutorial laravel get env variable in view. We will use laravel get env variable in the blade. This tutorial will give you a simple example of how to access the env variable in laravel blade. it's a simple example of how to get environment variable in laravel blade. So, let's follow a few step to create an example of how to get the env variable in laravel blade.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Set environment variable

APP_NAME=Nicesnippets

APP_ENV=local

APP_KEY=base64:****

APP_DEBUG=true

APP_URL=http://localhost

Get env Variable in Laravel Blade

This Step We Will Get Simply environment Variable Print in Blade file.

Example 1 :

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

Output :

"Nicesnippets"

Example 2 :

In This Step We Will Chack environment Variable.

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

Match

@endif

Output :

Match

Example 3 :

In this Step We Will Get Simply environment Variable using config function.

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

Output :

Nicesnippets

Example 4 :

In this Step We Will see how to get environment Variable in script teg.

<script>

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

console.log(name);

</script>

Output :

Nicesnippets

now it works...

I hope it can help you...