PHP code example of secretwebmaster / laravel-optionable

1. Go to this page and download the library: Download secretwebmaster/laravel-optionable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

secretwebmaster / laravel-optionable example snippets


use Illuminate\Database\Eloquent\Model;
use Secretwebmaster\LaravelOptionable\Traits\HasOptions;

class Post extends Model
{
    use HasOptions;  // <-- add this

    //...
}

$post = Post::first();

$post->getOptions(); // default: array
$post->getOptions('json'); // return JSON
$post->getOptions('collection'); // return Collection

$post->getOption('key');

$post->getOption('key', 'default');

$post->getOption('key', 'default', false);

$post->setOption('key', 'value');
$post->setOption('theme', ['color' => 'blue']); // arrays/objects supported (stored as JSON)

$post->setOptions([
    'language' => 'English',
    'mode' => 'dark',
    'homepage' => 'welcome',
]);

$post->deleteOption('key');

$post->deleteOptions(['key1', 'key2']);

$post->deleteAllOptions();

$post->deleteAllOptions(['language']); // deletes everything except 'language'

$post->get_option('key');
$post->set_option('key', 'value');
$post->delete_all_options();
bash
php artisan migrate