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();
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');