Adding new Settings

LaraBooking has a really simple way to manipulate settings. A setting is formed by a pair of a name (key) and a value. All the settings are stored in the settings table and are manipulated by the SettingsController.php. Let's learn how to get and add new settings.

1. Creating/Saving a Setting

By default, to save a setting, you can add it to the file resources/views/home/settings/fields.blade.php. For exemplification, let's consider we are adding the company_name setting. In the fields.blade.php file, we can add this code:

{!! Form::label('company_name', 'Company Name') !!}
{!! Form::text('company_name', settings('company_name'), ['class' => 'form-control']) !!}
                

If the setting is required or has a special validation, we can add it to the validator in the SettingsController@store method:

$request->validate([
    'company_name' => 'required|max:128',
    ...
]);
                

And then we set the parameter using the setValueByName method of the Settings model:

Settings::setValueByName('company_name', $request->get('company_name'));

You can pass array values too, the Settings model will save it automatically as a JSON string in the database:

$request->validate([
    'business_plans' => 'required|array',
    ...
]);

Settings::setValueByName('business_plans', $request->get('business_plans', []));
                

2. Get a Setting

You can use the helper settings($name, $defaultValue = '') to get a setting. In the $name you can pass the parameter name that you want to get the value. And you can pass a default value if you want.

Examples of existing settings:

settings('company_email'); // Returns the company e-mail

settings('company_name', 'My Company'); // Returns the company name with a default My Company

You can use Settings::getValueByName($name, $defaultValue = '') too if you wish to import the Settings model for this.