Skip to content

Commit

Permalink
added closure support to populate select options from database
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Feb 12, 2019
1 parent 8b89f4a commit 02f736a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions src/SavesSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -43,4 +44,9 @@ public function store(Request $request, AppSettings $appSettings)
'status' => config('app_settings.submit_success_message', 'Settings Saved.')
]);
}

protected function resolveClosers()
{

}
}
25 changes: 24 additions & 1 deletion src/Setting/AppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
{
Expand Down
26 changes: 26 additions & 0 deletions tests/SettingUITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace QCod\AppSettings\Tests\Feature;

use Illuminate\Support\Facades\DB;
use QCod\AppSettings\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 02f736a

Please sign in to comment.