I'm looking to store values from my database in my laravel config. I would store for example the site's title, the colors, etc.
I would like datas to be loaded in config only if they do not exist yet. When the data is modified, it will be deleted from config() so it will be load again.
Here is the code :
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (Schema::hasTable('settings')) {
if (!Config('settings')) {
Config::set('settings', \App\Models\Setting::all());
}
}
}
}
Of course I have a model and a table called "Setting".
The problem is : every time I load a page, it considers that config('settings') is empty and so it retrieves it from the database. I also tried with a singleton...
Do you have any idea why?
public function boot()
{
if (Schema::hasTable('settings')) {
if (!Config('settings')) {
foreach (\App\Models\Setting::all() as $setting) {
Config::set('settings.' . $setting->key, $setting->value);
}
}
}
}