🥰Define Global Variable In Laravel (ok)

https://www.scratchcode.io/best-ways-to-define-global-variable-in-laravel/

01 Using Service Provider

C:\xampp8\htdocs\lva\app\Providers\AppServiceProvider.php

C:\xampp8\htdocs\lva\resources\views\welcome.blade.php

02 Using Traits xem bài viết dưới

03 Using Config File

C:\xampp8\htdocs\plugindev\routes\web.php

C:\xampp8\htdocs\plugindev\config\global.php

C:\xampp8\htdocs\plugindev\resources\views\welcome.blade.php

01 Using Service Provider

Service providers in the laravel application are the central place where the application is bootstrapped. That is, laravel’s core services and our application’s services, classes, and their dependencies are injected into the service containers through providers.

Go to the app/Providers/AppServiceProvider.php and in boot()method add something like below:

Now, $user & $social variables are available globally everywhere.

02 Using Traits

Traits allow us to develop a reusable piece of code and inject it into the controller and modal in a Laravel application. So we can define methods in traits and call the methods globally in Controllers, and Models, and then we can pass the data to the views as we want.

Create A Trait

To create Traits first create a Traits folder in app/Http, then create Traits/GlobalTrait.php file, then place the entire code in app/Http/Traits/GlobalTrait.php like below:

Use Traits In Laravel

We can call the traits by using use keyword followed by the Trait name eg. use GlobalTrait.

And we can easily call the method by using $this->methodName()that’s it. Check the example below:

03 Using Config File

In Laravel, you can create a file in the config folder and create variables in that and use that across the application. For example, if we want to store some information like user roles, emails, external APIs credentials, social links, etc.

Let’s create a new config file name global.php in config/ directory. We will also add some global variables into it and their values so that you can access those variables globally in the Laravel app. Let’s do it.

Create Global Config File

config/global.php

Use Global Variable

After defining variables in the config file, it’s time to call it from anywhere. You can easily get value using config() helper. Let’s call it from the test route as below:

routes/web.php

Another way is to set the config on the go with key-value pair as below:

And get the values using below:

Notes: Make sure you always run the php artisan config:clear command after adding or removing variable/values from the config file.

Additionally, read our guide:

That’s it from our end. We hope this article helped you how to define global variables in Laravel.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

Last updated

Was this helpful?