How To Check Laravel Application Environment Tutorial

Reading Time: 5 minutes
915 Views

Inside this article we will see the concept i.e How To Check Laravel Application Environment Tutorial. Article contains the classified information about Way to Check Laravel Application Running Environment.

If you are looking for a solution i.e How to Check Laravel 9 Project Environment then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Also, we will cover i.e How To check for the application environment in Laravel views. To check your laravel application running in which environment like local, staging or production. Then there are several ways to do this.

We will use App::environment(), app()->environment(), @production and @env to check app current env.

Article is not for laravel specific version, you can use this concept with any laravel version.

Read More: Codeigniter 4 cURL GET Request Example Tutorial

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.

Create Controller

Open project into terminal and run this command.

$ php artisan make:controller DataController

It will create a DataController.php file inside /app/Http/Controllers folder.

Read More: How To Check Laravel Application Environment Tutorial

Example #1: Get Environment Using env()

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        echo env('APP_ENV');
    }
}

Here, output will be local for my application.

Example #2: Get Environment Using getenv()

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        echo getenv('APP_ENV');
    }
}

Here, Output will be local for my application.

Example #3: Get Environment Using App::environment()

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class DataController extends Controller
{
    public function index()
    {
        if (App::environment(['local', 'staging'])) {
            dd("This is Local or Staging server");
        }
    }
}

Here, Output will be This is Local or Staging server for my application.

App::environment([‘local’, ‘staging’]) It gets your application environment and then compares the environment value with the given array. If it matches then message will be printed.

Example #4: Get Environment Using app()->environment()

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        if (app()->environment(['local', 'staging'])) {
            dd("This is Local or Staging server");
        }
    }
}

Here, Output will be This is Local or Staging server for my application.

Read More: How To Only Allow Numbers in a Text Box Using jQuery

app()->environment([‘local’, ‘staging’]) It gets your application environment and then compares the environment value with the given array. If it matches then message will be printed.

Example #5: Get Environment In Laravel Views

Open any view file of your application and add this code into it.

@if(App::environment('production'))

    {{-- in "production" environment --}}

@endif

Also, we have this method as well.

@production

    {{-- in "production" environment --}}

@endproduction

We hope this article helped you to learn How To Check Laravel Application Environment Tutorial 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