PHP code example of signifly / laravel-configurable

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

    

signifly / laravel-configurable example snippets


// Remember to add use statement
use Signifly\Configurable\Configurable;

class User
{
    use Configurable;
    
    // Remember to make `config` fillable
    protected $fillable = [
        'config',
    ];
    
    // Remember to add `config` to casts
    protected $casts = [
        'config' => 'array',
    ];
}

Schema::table('users', function (Blueprint $table) {
    $table->json('config')->nullable();
});

$user = User::find(1);
$user->config()->some_key = 'some val';
$user->config()->set('some_other_key', 'some other val');
$user->save();

$user = User::find(1);
$user->config()->some_key; // returns some val
$user->config()->get('some_other_key'); // return some other val

$user = User::find(1);
$user->config()->remove('some_key');
$user->save();

$user = User::find(1);
$user->config()->has('some_key'); // returns true

$user = User::find(1);
$user->config()->collect('some_key'); // returns Collection(['some val']);

// Remember to add use statement
use Signifly\Configurable\Configurable;

class User
{
    use Configurable;
    
    // Remember to make `settings` fillable
    protected $fillable = [
        'settings', 'extras',
    ];
    
    // Remember to add `settings` to casts
    protected $casts = [
        'settings' => 'array',
        'extras' => 'array',
    ];

    protected function getConfigKey()
    {
        return 'settings';
    }

    // or add a custom config attribute like this:
    public function getExtrasAttribute()
    {
        return new Config($this, 'extras');
    }
}