PHPackages                             remzikocak/laravel-options - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. remzikocak/laravel-options

ActiveLibrary

remzikocak/laravel-options
==========================

Database Options/Settings Package for Laravel 9/10/11/12.

2.3.0(1mo ago)93.2k1MITPHPPHP ^8.1CI failing

Since Apr 23Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/remzikocak/laravel-options)[ Packagist](https://packagist.org/packages/remzikocak/laravel-options)[ RSS](/packages/remzikocak-laravel-options/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (8)Versions (25)Used By (0)

[![Laravel Options](/laravel-options.png)](/laravel-options.png)

Laravel Options Package
=======================

[](#laravel-options-package)

This Package will help you to dynamically add Option fields to your Backend Panel.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require remzikocak/laravel-options
```

The Package will automatically register the Service Provider and Facade. Afterwards, you need to publish and migrate:

```
php artisan vendor:publish --provider="RKocak\Options\OptionsServiceProvider"
php artisan migrate
```

This will create the migration files and options.php file in your config folder.

Usage
-----

[](#usage)

First of all, you need to create a Optiongroup and an Option.

```
use RKocak\Options\Models\Optiongroup;
use RKocak\Options\Models\Option;

Optiongroup::create([
    'label'         => 'My Optiongroup',
    'description'   => 'Optiongroup description', // can be null
    'display_order' => 1,
]);

Option::create([
    'name'          => 'myOption' // should be unique
    'label'         => 'My Option',
    'description'   => 'My awesome Option',
    'value'         => 'Option value',
    'type'          => 'text',
]);
```

Option groups and options have a many-to-many relationship. Groups can have many options, and options can belong to many groups. Understanding this relationship is essential for displaying them in your backend panel.

For assignment, use the following:

```
$group = Optiongroup::find(1);
$option = Option::find(1);

// Assign option to a group
$group->options()->attach($option);

// or..
$option->groups()->attach($group);
```

Getting options
---------------

[](#getting-options)

To retrieve the computed value, use the `getValue()` method from the Option Model.

```
$option->getValue();

// This will be the "raw" value that is stored in the database
$option->value
```

A better and more performant option is to utilize the `Options` Facade. This will cache the options when you only require a key =&gt; value store.

You can use it as following:

```
Options::get('optionName');

// You can pass a second parameter as default value
Options::get('optionName', null);

// or use the helper function
options('optionName', null);
```

Check if option with the given name exists

```
Options::has('optionName');
```

While the cache usually refreshes automatically, if you need to do it manually, use the following method.

```
Options::getLoader()->rebuildCache();
```

Adding custom Types
-------------------

[](#adding-custom-types)

To add your custom type, you need to create a class that extends ` RKocak\Options\Type`

Example:

```
