Last active
April 5, 2018 08:49
-
-
Save Neoglyph/485000ef3a126b7f158e404d6a2f0ce2 to your computer and use it in GitHub Desktop.
Create dynamic config entries from database - Laravel (5.5/5.6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Laravel 5.5/5.6 | |
// AppServiceProvider | |
public function boot() { | |
// If we have a database table with configs... | |
if (Schema::hasTable('table_with_config')) { | |
// Get all database stored configs | |
// DatabaseConfigs is a Model | |
$configs = \Cache::rememberForever('configs', function() { | |
return DatabaseConfigs::all(); | |
}); | |
// Set config for all database configs | |
foreach ($configs as $dbConfig) { | |
$config = $dbConfig->config; | |
// Merge the fixed config file with the database config values | |
if ($fixedConfig = \Config::get($dbConfig->name)) { | |
$config = array_merge($fixedConfig, $config); | |
} | |
\Config::set($dbConfig->name, $config); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment