Display a text based on Laravel environment or mode

- Andrés Cruz

En español

Display a text based on Laravel environment or mode

Laravel has three types of environment: local, production, and test. The local environment is used for application development, while the production environment is used to host the application for production. The test environment is used to run automated tests on the application.

Each environment has its own configuration file in the config folder of the Laravel project. These files allow you to set different configuration values for each environment, allowing greater flexibility in configuring the application. For example, the database can be configured to use different user credentials in the local versus production environment.

In general, the use of different environments in Laravel allows you to maintain a high degree of control and flexibility over the configuration of the application, which in turn can improve the quality of the software and the efficiency of the development team.

A very common task is to want to show or hide a text, or any HTML element... or even start or stop a process depending on the environment we are in and we can easily do this by consulting the status or environment of our project.

You have a few options here. The best way to do this is using the project's configuration file, it's the cleanest and easiest. However, you can also ask Laravel directly what the current environment is.

The configuration file is based on the configuration set in your .env and you can access it as such: for that we have several ways as you can see in the following example:

// view
@if (config('app.env') === 'production')
   Texto en Produccion
@endif

// view
@if (app()->environment() === 'production')
   Texto en Produccion
@endif
// O
@if (app()->environment('production'))
   Texto en Produccion
@endif

So, if you want to display content according to the environment in which you are, you already know how to do it.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.