Laravel Option Framework is a complete framework for managing your laravel application's dynamic settings in one place with various supported input types. Options are needed in every dynamic website or web application like making dynamic application title, social media links, header-footer dynamic background colour and etc. In Laravel, we make our necessary application option management tool from scratch. Now the Laravel Option Framework made the task easier. Just add the option configuration and you are ready to go! It'll take care of all your option management stuff.
composer require haruncpi/laravel-option-framework
Now run the command for publishing vendor resources.
php artisan vendor:publish --provider="Haruncpi\LaravelOptionFramework\ServiceProvider"
Run migration
php artisan migrate
or create an options table manually
CREATE TABLE `options` (
`option_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`option_value` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`option_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
With Laravel Option Framework, you can manage your application options in 2 ways.
Add an options.php
file in your config directory with your necessary option field configuration.
Demo option configuration.
<?php
// config/options.php
return array(
array(
"id" => "general",
"label" => "General",
"icon" => "fa-cubes",
"fields" => array(
array(
"type" => "text",
"id" => "site_name",
"label" => "Site Name",
"description" => "Enter your site name",
"icon" => "fa-globe",
"validation" => 'required|min:10'
),
array(
"type" => "text",
"id" => "site_slogan",
"label" => "Site Slogan",
"description" => "Enter site slogan",
"validation" => 'required'
),
array(
"type" => "timepicker",
"id" => "backup_time",
"label" => "Backup Time",
"description" => "Set db backup time",
"validation" => 'required'
)
)
),
array(
"id" => "social",
"label" => "Social",
"icon" => "fa-globe",
"fields" => array(
array(
"type" => "text",
"id" => "fb_link",
"label" => "Facebook",
"description" => "Enter facebook link",
"icon" => "fa-facebook-square"
),
array(
"type" => "text",
"id" => "twitter_link",
"label" => "Twitter",
"description" => "Enter twitter link",
"icon" => "fa-twitter-square"
)
)
)
);
Now add it on your route file.
Route::optionRoutes();
After adding this route, you will get UI in example.com/admin/options
.
To retrieve an option value just call getOption('your_option_name')
Required at least one option group.
id | mandatory | A unique id without space. Ex: social_links |
label | mandatory | String |
icon | mandatory | font awesome icon name. Ex: fa-globe |
fields | mandatory | An array of options field where each field an array. |
Field: text
array(
"type" => "text",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"placeholder" => "Placeholder",
"icon" => "fa-globe",
"validation" => 'required|min:22'
)
Field: textarea
array(
"type" => "textarea",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: switcher
array(
"type" => "switcher",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: editor
array(
"type" => "editor",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: colorpicker
array(
"type" => "colorpicker",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: datepicker
array(
"type" => "datepicker",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: timepicker
array(
"type" => "timepicker",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: datetimepicker
array(
"type" => "datetimepicker",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: dropdown - from the database table
array(
"type" => "dropdown",
"id" => "unique_option_name",
"options" => "users,id,name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: dropdown - from a simple array
array(
"type" => "dropdown",
"id" => "unique_option_name",
"options" => ["Male"=>"Male","Female"=>"Female"],
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: autocomplete - from the database table
array(
"type" => "autocomplete",
"id" => "unique_option_name",
"options" => "users,id,name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: radio
array(
"type" => "radio",
"id" => "unique_option_name",
"options" => ["BD", "IND", "PAK", "ENG"],
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: tag
array(
"type" => "tag",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: multicheck
array(
"type" => "multicheck",
"id" => "unique_option_name",
"options" => ["Cricket", "Hocky", "Foodball"],
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Field: icon
array(
"type" => "icon",
"id" => "unique_option_name",
"label" => "Option Label Name",
"description" => "Description",
"icon" => "icon",
"validation" => 'validation rules'
)
Manage options in your way and logic with laravel option framework helper functions. In this way, you have to care about all validation, UI making and so on.
Create an option
createOption('site_title','Laravel Article');
Get an option value
getOption('site_title');
// Laravel Article
Checking an option exist
optionExist('site_title');
// true
Update an option
updateOption('site_title','Laravel Article');
//true
Delete an option
deleteOption('foo');
// true
Laravel option framework gives you the flexibility to change option route path, admin panel route path, custom middleware, default icon, etc. If you need to change these configurations then open the option-framework.php
file from the config directory and change what you need to change.