Skip to content

Commit 02f736a

Browse files
committed
added closure support to populate select options from database
1 parent 8b89f4a commit 02f736a

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,24 @@ A select box can be defined with options:
289289
],
290290
```
291291

292+
### Select options from database
293+
294+
You can also populate select options dynamically. In most cases it will be coming from the database, just use a closure for this:
295+
296+
```php
297+
[
298+
'type' => 'select',
299+
'name' => 'city',
300+
'label' => 'City',
301+
'rules' => 'required',
302+
'options' => function() {
303+
return App\City::pluck('name', 'id')->toArray()
304+
}
305+
],
306+
```
307+
308+
> Note: You can use a closure (anonymous function) for most of the fields on inputs if you need to resolve field value dynamically.
309+
292310
#### boolean
293311

294312
Boolean is just a radio input group with yes or no option, you can also change it to select by setting `options` array:

src/SavesSettings.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ trait SavesSettings
1414
* Display the settings page
1515
*
1616
* @return \Illuminate\View\View
17+
* @param AppSettings $appSettings
1718
*/
18-
public function index()
19+
public function index(AppSettings $appSettings)
1920
{
20-
$settingsUI = config('app_settings', []);
21+
$settingsUI = $appSettings->loadConfig(config('app_settings', []));
2122
$settingViewName = config('app_settings.setting_page_view');
2223

2324
return view($settingViewName, compact('settingsUI'));
@@ -43,4 +44,9 @@ public function store(Request $request, AppSettings $appSettings)
4344
'status' => config('app_settings.submit_success_message', 'Settings Saved.')
4445
]);
4546
}
47+
48+
protected function resolveClosers()
49+
{
50+
51+
}
4652
}

src/Setting/AppSettings.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ public function save($request)
123123
$this->cleanUpSettings($allDefinedSettings);
124124
}
125125

126+
/**
127+
* Load and resolve closers in config
128+
*
129+
* @param $config
130+
* @return array
131+
*/
132+
public function loadConfig($config)
133+
{
134+
// resolve any callback for inputs
135+
array_walk_recursive($config, function (&$value, $key) {
136+
if (!in_array($key, ['mutator', 'accessor', 'rules']) && is_callable($value)) {
137+
// skip any string which dont look like namesapce
138+
if (is_string($value) && str_contains($value, '\\') == false) {
139+
return;
140+
}
141+
142+
$value = $this->runCallback($value, $key, '');
143+
}
144+
});
145+
146+
return $config;
147+
}
148+
126149
/**
127150
* Get the settings UI sections as collection
128151
*
@@ -211,7 +234,7 @@ private function castValue($dataType, $value, $out = false)
211234
* @param $callback
212235
* @param $name
213236
* @param $value
214-
* @return string
237+
* @return string|mixed
215238
*/
216239
protected function runCallback($callback, $name, $value)
217240
{

tests/SettingUITest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace QCod\AppSettings\Tests\Feature;
44

5+
use Illuminate\Support\Facades\DB;
56
use QCod\AppSettings\Tests\TestCase;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78

@@ -173,6 +174,31 @@ public function it_shows_select_input_with_options()
173174
->assertSee('IN');
174175
}
175176

177+
/**
178+
* it can populate options from database dynamically
179+
*
180+
* @test
181+
*/
182+
public function it_can_populate_options_from_database_dynamically()
183+
{
184+
$this->configureInputs([
185+
[
186+
'name' => 'app_migrations',
187+
'label' => 'App Migration',
188+
'type' => 'select',
189+
'options' => function () {
190+
return DB::table('migrations')->pluck('migration', 'id')->toArray();
191+
}
192+
]
193+
]);
194+
195+
// assert
196+
$this->get('/settings')
197+
->assertStatus(200)
198+
->assertSee('select')
199+
->assertSee('2014_10_00_000000_create_settings_table');
200+
}
201+
176202
/**
177203
* it shows textarea input
178204
*

0 commit comments

Comments
 (0)