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?
1 Answer
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
session()->set('foo', 'bar')andsession()->get('foo')Redis, it already has support out of the box in Laravel.getenv()