qcod / laravel-app-settings

Store settings in database with a manager UI for your Laravel app
MIT License
338 stars 53 forks source link

Custom controller not working #8

Closed JBou closed 5 years ago

JBou commented 5 years ago

if I change the controller setting it doesn't have any effect because the controller in the routes are hardcoded

// Controller to show and handle save setting 'controller' => '\QCod\AppSettings\Controllers\AppSettingController'

https://github.com/qcod/laravel-app-settings/blob/4197ed8e6851eba4898ca226c9f343cb529adda5/src/routes/web.php#L3-L10

saqueib commented 5 years ago

@JBou Thanks for pointing it out.

Would you please tell me why you need to change the controller. What do you want to solve?

saqueib commented 5 years ago

Its fixed in 1.0.3 docs here

JBou commented 5 years ago

I wanted to populate a select input using values from the database, so I needed to dynamically set the select options in the config.

Maybe there is a better way to change the values shown on the settings page programatically instead of hardcoding them in the config files?

class AppSettingController extends \QCod\AppSettings\Controllers\AppSettingController
{
    /**
     * Display the settings page
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $years = Year::all()->mapWithKeys(function ($item, $key) {
            return [$item->year => $item->year];
        })->all();
        config(['app_settings.sections.app.inputs.0.options' => $years]);
        return parent::index();
    }
}
saqueib commented 5 years ago

Thanks, really this is a good feature to add, I will do it soon.

saqueib commented 5 years ago

Its done 🎉

Now you can pass a closure in options to populate all the options from database:

[
    'type' => 'select',
    'name' => 'year',
    'label' => 'Year',
    'rules' => 'required',
    'options' => function() {
        return Year::pluck('year', 'year')->toArray()
    }
],

Have fun coding 👍