PHP code example of a1383n / laravel-eloquent-settings

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

    

a1383n / laravel-eloquent-settings example snippets


use Illuminate\Database\Eloquent\Model;
use LaravelEloquentSettings\Enums\SettingValueType;
use LaravelEloquentSettings\Contracts\HasSettingsInterface;
use LaravelEloquentSettings\HasSettings;

class User extends Model implements HasSettingsInterface
{
    use HasSettings;

    public function definedSettings(SettingDefinition $definition): void
    {
        $definition->define('locale')
            ->type(SettingValueType::STRING)
            ->default('fa-IR');

        $definition->define('extra')
            ->type(SettingValueType::ARRAY)
            ->nullable();
    }
}

use App\Models\User;

$user = User::findOrFail(1);
$user->getSettingValueByName('locale');
$user->setSettingValueByName('locale', 'en-US');

use LaravelEloquentSettings\EloquentSettings;
use LaravelEloquentSettings\SettingResolver;
use LaravelEloquentSettings\SettingSetter;
use App\Models\User;

$handler = EloquentSettings::getHandler(User::find(1));

$resolver = new SettingResolver($handler);
$value = $resolver($settings->get('locale'));

$setter = new SettingSetter($handler);
$setter('extra', ['foo' => 'bar']);
bash
php artisan vendor:publish --provider=LaravelEloquentSettings\EloquentSettingsServiceProvider
bash
php artisan migrate