PHP code example of mxrck / laravel-dboptions

1. Go to this page and download the library: Download mxrck/laravel-dboptions 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/ */

    

mxrck / laravel-dboptions example snippets


// Get an option from the database or fallback to default
Options::get( 'somekey', 'default value' ); // if you need to reload from database then pass a third boolean argument with true

// Update or create (if not exists) an option
Options::update( 'somekey', 'somevalue' ); // If you want to autoload the option then pass a third array argument like ['autoload' => true]

// Check if an option is already stored in the database
Options::exists( 'somekey' );

// Delete an option from the database
Options::remove( 'somekey' );

// Get all options as array
Options::all();

/* this method return an array like this */
$all_options = [
    'somekey' => [
        'value'     => 'THE_VALUE_STORED',
        'autoload'  => false,
        'public'    => false
    ],
    ...
];

// Get all public options as array
Options::public(); // same as before, but only public options

// Get options instance
$options = option();
/* With $options you have all facade methods available to use */
/*
 * options()->get(...); options()->update(...); options()->exists(...); ...
 */
 
 // Get and option from database or fallback to default
 option('somekey')
 
 // Update or create (if not exists) an option
 option_update( 'somekey', 'somevalue' );
 
 // Check if option exists
 option_exists( 'somekey' );
 
 // Delete an option
 option_remove( 'somekey' );

// User.php
use Illuminate\Database\Eloquent\Model;

class User extends Model implements OptionableInterface
{
    use OptionableTrait;
}

// Provider
use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'user' => 'App\User'
]);

// User.php
use Illuminate\Database\Eloquent\Model;

class User extends Model implements OptionableInterface
{
    use OptionableTrait;
    
    public function getType(): string
    {
        return 'user';
    }
}

$user = User::find(10);

$user->option( 'somekey' ); // By default, from an optionable instance you can call options with the optionable context

// Or you can use the facade and helper with custom context
$value = Option::context( option_context( $user ) )->get( 'somekey' );
// Or
$value = Option::context( Context::make( $user ) )->get( 'somekey' );
// Or
$value = Option::context( $user )->get( 'somekey' );
// Or
$value = option( option_context( $user ) )->get( 'somekey' );
// Or
$value = option( $user )->get( 'somekey' );

option( $user )->get( 'some_option' );
// return null

option( 'some_option');
// return null

option( $user )->update( 'some_option', 'user_value' );
// return 'user_value'
option( $user )->get( 'some_option' );
// return 'user_value'

option()->update( 'some_option', 'system_value' );
// return 'system_value'
option( 'some_option' )
// return 'system_value'

echo 'System: ' option( 'some_option' ) . ' User: '. option( $user )->get( 'some_option' );
// System: system_value User: user_value

// Fallback example:

option()->update( 'color', '#fffff' );
option( option_context( $user ) )->get( 'color' );
// return null
option( option_context( $user, true ) )->get( 'color' );
// return #ffffff

// Or using your model:
$user->optionFallback( 'color' );
// return #ffffff

// By default, context fallback is false
bash
php artisan option:update {KEY} {VALUE}
bash
php artisan option:get {KEY}