PHP code example of hosmelq / laravel-model-preferences

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

    

hosmelq / laravel-model-preferences example snippets


use HosmelQ\ModelPreferences\Contracts\HasPreferences;
use HosmelQ\ModelPreferences\Models\Concerns\InteractsWithPreferences;
use HosmelQ\ModelPreferences\Support\PreferencesConfig;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements HasPreferences
{
    use InteractsWithPreferences;

    public function preferencesConfig(): PreferencesConfig
    {
        return PreferencesConfig::configure();
    }
}

use HosmelQ\ModelPreferences\Support\PreferencesConfig as ModelPreferencesConfig;
use Illuminate\Validation\Rule;

public function preferencesConfig(): ModelPreferencesConfig
{
    return ModelPreferencesConfig::configure()
        ->withDefaults([
            'notifications' => true,
            'theme' => 'system',
        ])
        ->withRules([
            'notifications' => ['boolean'],
            'theme' => [Rule::in(['dark', 'light', 'system'])],
        ]);
}

use HosmelQ\ModelPreferences\Support\PreferencesConfig;

return PreferencesConfig::configure()
    ->withDefaults([
        'theme' => 'system',
    ]);

use App\Models\User;
use HosmelQ\ModelPreferences\Exceptions\PreferenceValidationException;

$user = User::query()->firstOrFail();

try {
    $user->preferences()->set('theme', 'invalid');
} catch (PreferenceValidationException $e) {
    $errors = $e->errors();
}

use App\Models\User;

$user = User::query()->firstOrFail();

$preferences = $user->preferences();

use App\Models\User;

$user = User::query()->firstOrFail();

$theme = $user->preferences()->get('theme');
$timezone = $user->preferences()->get('timezone', 'UTC');
$locale = $user->preferences()->get('locale', fn () => app()->getLocale());
$values = $user->preferences()->getMultiple(['theme', 'timezone']);

use App\Models\User;

$user = User::query()->firstOrFail();

$user->preferences()->set('theme', 'dark');
$user->preferences()->setMultiple([
    'theme' => 'dark',
    'locale' => 'en',
]);

use App\Models\User;

$user = User::query()->firstOrFail();

if ($user->preferences()->has('theme')) {
    // ...
}

if ($user->preferences()->missing('timezone')) {
    // ...
}

use App\Models\User;

$user = User::query()->firstOrFail();

$all = $user->preferences()->all();

use App\Models\User;

$user = User::query()->firstOrFail();

$user->preferences()->delete('theme');
$user->preferences()->deleteMultiple(['theme', 'locale']);
$user->preferences()->clear();

use App\Models\User;

enum UserPreference: string
{
    case Theme = 'theme';
}

$user = User::query()->firstOrFail();

$user->preferences()->set(UserPreference::Theme, 'dark');

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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

use HosmelQ\ModelPreferences\Support\PreferencesConfig;

return PreferencesConfig::configure()
    ->withDriver('table')
    ->withTable('users_preferences');

use HosmelQ\ModelPreferences\Contracts\HasPreferences;
use HosmelQ\ModelPreferences\Contracts\PreferenceDriver;

class CustomDriver implements PreferenceDriver
{
    public function all(HasPreferences $model): array
    {
        return [];
    }

    public function clear(HasPreferences $model): void
    {
        // ...
    }

    public function delete(HasPreferences $model, string $key): void
    {
        // ...
    }

    public function deleteMultiple(HasPreferences $model, array $keys): void
    {
        // ...
    }

    public function get(HasPreferences $model, string $key): mixed
    {
        return null;
    }

    public function getMultiple(HasPreferences $model, array $keys): array
    {
        return [];
    }

    public function has(HasPreferences $model, string $key): bool
    {
        return false;
    }

    public function missing(HasPreferences $model, string $key): bool
    {
        return true;
    }

    public function set(HasPreferences $model, string $key, mixed $value): void
    {
        // ...
    }

    public function setMultiple(HasPreferences $model, array $values): void
    {
        // ...
    }
}

use App\Preferences\CustomDriver;
use HosmelQ\ModelPreferences\PreferencesManager;
use HosmelQ\ModelPreferences\PreferencesStore;

app(PreferencesManager::class)->extend('custom', function () {
    return new PreferencesStore('custom', new CustomDriver());
});

use HosmelQ\ModelPreferences\Support\PreferencesConfig;

return PreferencesConfig::configure()
    ->withDriver('custom');
bash
php artisan model-preferences:install
bash
php artisan vendor:publish --tag="model-preferences-config"
php artisan vendor:publish --tag="model-preferences-migrations"
bash
php artisan migrate
bash
php artisan model-preferences:table users_preferences