diff --git a/README.md b/README.md index 6d63895..978e83f 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,24 @@ A select box can be defined with options: ], ``` +### Select options from database + +You can also populate select options dynamically. In most cases it will be coming from the database, just use a closure for this: + +```php +[ + 'type' => 'select', + 'name' => 'city', + 'label' => 'City', + 'rules' => 'required', + 'options' => function() { + return App\City::pluck('name', 'id')->toArray() + } +], +``` + +> Note: You can use a closure (anonymous function) for most of the fields on inputs if you need to resolve field value dynamically. + #### boolean Boolean is just a radio input group with yes or no option, you can also change it to select by setting `options` array: diff --git a/src/SavesSettings.php b/src/SavesSettings.php index 7d6d740..c342f83 100644 --- a/src/SavesSettings.php +++ b/src/SavesSettings.php @@ -14,10 +14,11 @@ trait SavesSettings * Display the settings page * * @return \Illuminate\View\View + * @param AppSettings $appSettings */ - public function index() + public function index(AppSettings $appSettings) { - $settingsUI = config('app_settings', []); + $settingsUI = $appSettings->loadConfig(config('app_settings', [])); $settingViewName = config('app_settings.setting_page_view'); return view($settingViewName, compact('settingsUI')); @@ -43,4 +44,9 @@ public function store(Request $request, AppSettings $appSettings) 'status' => config('app_settings.submit_success_message', 'Settings Saved.') ]); } + + protected function resolveClosers() + { + + } } diff --git a/src/Setting/AppSettings.php b/src/Setting/AppSettings.php index 7718f8f..6a6f03d 100644 --- a/src/Setting/AppSettings.php +++ b/src/Setting/AppSettings.php @@ -123,6 +123,29 @@ public function save($request) $this->cleanUpSettings($allDefinedSettings); } + /** + * Load and resolve closers in config + * + * @param $config + * @return array + */ + public function loadConfig($config) + { + // resolve any callback for inputs + array_walk_recursive($config, function (&$value, $key) { + if (!in_array($key, ['mutator', 'accessor', 'rules']) && is_callable($value)) { + // skip any string which dont look like namesapce + if (is_string($value) && str_contains($value, '\\') == false) { + return; + } + + $value = $this->runCallback($value, $key, ''); + } + }); + + return $config; + } + /** * Get the settings UI sections as collection * @@ -211,7 +234,7 @@ private function castValue($dataType, $value, $out = false) * @param $callback * @param $name * @param $value - * @return string + * @return string|mixed */ protected function runCallback($callback, $name, $value) { diff --git a/tests/SettingUITest.php b/tests/SettingUITest.php index 4b806c5..9533876 100644 --- a/tests/SettingUITest.php +++ b/tests/SettingUITest.php @@ -2,6 +2,7 @@ namespace QCod\AppSettings\Tests\Feature; +use Illuminate\Support\Facades\DB; use QCod\AppSettings\Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -173,6 +174,31 @@ public function it_shows_select_input_with_options() ->assertSee('IN'); } + /** + * it can populate options from database dynamically + * + * @test + */ + public function it_can_populate_options_from_database_dynamically() + { + $this->configureInputs([ + [ + 'name' => 'app_migrations', + 'label' => 'App Migration', + 'type' => 'select', + 'options' => function () { + return DB::table('migrations')->pluck('migration', 'id')->toArray(); + } + ] + ]); + + // assert + $this->get('/settings') + ->assertStatus(200) + ->assertSee('select') + ->assertSee('2014_10_00_000000_create_settings_table'); + } + /** * it shows textarea input *