0

I need to set global variables in Laravel that I can modify whenever I want. Suppose that I have an admin area where I want to be able to modify colors, ads ... etc. How can I do that, without storing values in the database?

5
  • Use sessions, session()->set('foo', 'bar') and session()->get('foo') Commented Sep 9, 2017 at 17:19
  • You can use in-memory databases like Redis, it already has support out of the box in Laravel. Commented Sep 9, 2017 at 18:02
  • Is there any other better ways , without storing , updating and retrieving data from database ? Commented Sep 9, 2017 at 21:09
  • You could use environment variables as well, and then access them with getenv() Commented Sep 10, 2017 at 2:33
  • Honestly, based on what you're describing. I think it's the best (and easiest) solution to use a database. Last thing you want to do is deal with issues when you (will eventually) need to clear sessions/cache/redis, etc. I wouldn't overthink it IMHO. Commented Sep 10, 2017 at 8:21

1 Answer 1

1

You can use the config.

For the example, I am using Jenssegers\Agent\Agent to detect is the client desktop and generate different views and different queries. For that, I have created config/globalVars.php:

use Jenssegers\Agent\Agent;
$agent = new Agent();

return [
    'isDesktop' => $agent->isDesktop()
];

Now, I can use config('globalVars.isMobile') everywhere.

In your case, you want and to change these vars:

    var_dump(config('globalVars.isDesktop')); // true/false

    config(['globalVars' => ['isDesktop' => 'fake info']]);

    var_dump(config('globalVars.isDesktop')); // fake info
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.