0

I have a variable.

$key_test = 123456789;

I want to be able to access this 1 variable anywhere in my app even in config files, models, controllers, and views.

I've tried adding it in my boot():

<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\VSE, App\CURL;
use View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        $key_test = 123456789;

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

I've tried to access it in one of my config file. I kept getting null.

<?php

dd($key_test); <---- null

return [ ... ]; 

How do I access that variable in my config file?

Did I do it right in my boot()?

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • You can save it in cookies or sessions as you required – M Maavia Mar 14 '17 at 15:28
  • How do I do that ? Where do I do that ? in my boot() in my Service Provider file ? Feel free to give more hints or answer. – code-8 Mar 14 '17 at 15:46
  • that's depends upon your flow or logic where is best you can use it – M Maavia Mar 14 '17 at 15:50
  • This is my flow. I need to query my database first, get the result of that, store in a variable, and share that variable. How can I achieve that ? If you can help me with that, you save my life. :) – code-8 Mar 14 '17 at 15:52
  • in my case if i have login application than i will save it on success login if i have solo application than i will do it in constructor of class and make object of it in each controller – M Maavia Mar 14 '17 at 15:53
  • if you make database call every time it make your application slow – M Maavia Mar 14 '17 at 15:53
  • ouch why so complicated ... – code-8 Mar 14 '17 at 15:53
  • not soo complicated it is the simplest way – M Maavia Mar 14 '17 at 15:54
  • How can I do that only time not everytime ? How I just want to access that variable in one of my config file ... – code-8 Mar 14 '17 at 15:54

3 Answers3

3

Put this variable into the .env file:

VAR=123456789;

Then you'll be able to access it with env('VAR'); from config files, models, controllers and other classes.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I like your suggestion about .env file. But what if I need to query my database first, get the result of that, store in a variable, and share that variable. How can I achieve that in .env ? Do you have other suggestions fo me ? – code-8 Mar 14 '17 at 15:42
  • 2
    .env are only really good for static variables, you might be better off using the `config()` helper for site wide configurations. – Ian Mar 14 '17 at 15:43
  • @ihue in this case just set variable to the config with `config(['var' => 123456789])` and then you'll be able to get the variable value with `config('var')`. But it won't work in you want to get this value in config files. – Alexey Mezenin Mar 14 '17 at 16:29
  • @lan I agree that `config()` is better, but OP asked about getting this variable even in config files. `config()` won't work here, only `env()` will. – Alexey Mezenin Mar 14 '17 at 16:30
3

You can use a helper method for config.

config(['key_test'=>123456789])

Then access it through the same way,

config('key_test')
Ian
  • 3,539
  • 4
  • 27
  • 48
1

you have some choice here, for global variable, session, file, db, or in the memory with redis or memcache.

LF00
  • 27,015
  • 29
  • 156
  • 295
  • I like your suggestion aboutglobal variable, session, file, db, or in the memory with redis or memcache. But what if I need to query my database first, get the result of that, store in a variable, and share that variable. Can I still achive when doing one of those above that you mentioned. If so, where should I do that ? in what file ? in my service provicer ? – code-8 Mar 14 '17 at 15:43