ItSolutionStuff.com

How to Define Global Variables in Laravel?

By Hardik Savani • November 5, 2023
Laravel

Laravel is an awesome framework today, providing several good features, which is why it is a very popular PHP framework. When working on big website projects or ERP level projects, we need to define global variables like site title, pagination number, footer text, etc., depending on the project. In this tutorial, I will show you how to define and use global variables in Laravel, which provides a simple way to set global variables in our Laravel application with a good directory structure.

To define global variables, we need to create a global.php file in the config folder of your Laravel application. In this example, I have added three variables to the file: siteTitle, pagination, and tagLine, but you can add more. Once defined, you can access these global variables throughout the application, including in the controller, view, model, and anywhere else. Copy the code below and paste it into the global.php file.

config/global.php

return [

'siteTitle' => 'HD Site',

'pagination' => 5,

'tagLine' => 'Do the best'

];

After creating this file you can use using config() helper. in bellow line you can put anywhere and get "siteTitle" value, same way you can get other value too.

Example:

{{ config('global.siteTitle') }}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Global Variable for All Views Example

Read Now →

Laravel Google ReCaptcha V2 Example Tutorial

Read Now →

Laravel Custom Forgot & Reset Password Example

Read Now →

Laravel Add Custom Configuration File Example

Read Now →

Laravel Livewire Toastr Notifications Example

Read Now →

Laravel Telescope Installation and Configuration Tutorial

Read Now →

Laravel Carbon addMinutes() | Laravel Carbon Add Minutes Example

Read Now →

Laravel Carbon addYears() | Laravel Carbon Add Year Example

Read Now →

Laravel Signature Pad Example Tutorial

Read Now →

Laravel Global Scope Tutorial Example

Read Now →

How to Pass Data to All Views using Composer Share in Laravel?

Read Now →

How to Set Config Value Dynamically in Laravel?

Read Now →

How to Get Config Variable Value in Laravel?

Read Now →