### Install Package & Run Migrations Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Installs the package and optionally runs the necessary database migrations. The install command automates publishing and running migrations, config files, and prompts for setup. ```bash php artisan model-preferences:install ``` -------------------------------- ### Install Package Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Installs the Laravel Model Preferences package using Composer. This is the primary method for adding the package to your Laravel project. ```bash composer require hosmelq/laravel-model-preferences ``` -------------------------------- ### Model Setup with Defaults Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md An example of setting up another Eloquent model, `Team`, to manage preferences. It includes the `HasPreferences` interface, `InteractsWithPreferences` trait, and defines default values for `max_members` and `visibility`. ```php 10, 'visibility' => 'private', ]; } } ``` -------------------------------- ### Set and Get Preferences Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Shows basic usage for setting and retrieving preferences on a model. It covers setting a single preference, getting a preference, and retrieving a preference with a custom default value. ```php $user = User::find(1); // Set a preference $user->setPreference('theme', 'light'); // Get a preference $theme = $user->preference('theme'); // 'light' // Get with custom default $theme = $user->preference('theme', 'dark'); ``` -------------------------------- ### Getting Preferences (Individual, All, Existence) Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Details methods for retrieving preferences. It shows getting a single preference with or without a custom default, retrieving all merged preferences, and checking if a preference exists. ```php $notifications = $user->preference('notifications'); // Returns stored value or default $theme = $user->preference(UserPreference::Theme, 'light'); // Custom default using enum $preferences = $user->allPreferences(); if ($user->hasPreference('notifications')) { // Handle preference } if ($user->hasPreference(UserPreference::Theme)) { // Using enum // Handle preference } ``` -------------------------------- ### Run Package Tests Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Executes the package's test suite using Composer. This command ensures that the package is functioning as expected and helps in verifying any changes or contributions. ```Bash composer test ``` -------------------------------- ### Publish Configuration File Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Publishes the package's configuration file to your application's config directory. This allows for customization of package settings. ```bash php artisan vendor:publish --tag="model-preferences-config" ``` -------------------------------- ### Run Migrations Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Executes all pending database migrations, including those published by the model-preferences package. This command is essential after publishing migrations. ```bash php artisan migrate ``` -------------------------------- ### Setting Preferences (Individual and Multiple) Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Illustrates how to set preferences on a model. It covers setting a single preference, including using an enum for the key, and setting multiple preferences simultaneously using an array. ```php $team = Team::find(1); $team->setPreference('max_members', 50); $team->setPreference(TeamPreference::Visibility, 'public'); // Using enum $team->setPreferences([ 'max_members' => 100, 'visibility' => 'public', ]); ``` -------------------------------- ### Publish Migrations Manually Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Publishes the package's migration files to your application's database directory. This allows for manual control over when migrations are run. ```bash php artisan vendor:publish --tag="model-preferences-migrations" ``` -------------------------------- ### Implement HasPreferences Interface and Trait Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Demonstrates how to integrate the package into an Eloquent model. It requires implementing the `HasPreferences` interface and using the `InteractsWithPreferences` trait, along with defining default preferences. ```php true, 'theme' => 'system', ]; } } ``` -------------------------------- ### Eager Loading Preferences Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Demonstrates how to eager load preferences to optimize database queries, preventing N+1 issues. It shows loading specific preferences or all preferences into the model's relationship cache for faster subsequent access. ```php // Load specific preferences $user->loadPreferences([ 'notifications', UserPreference::Theme // Using enum ]); // Load all preferences $user->loadPreferences(); // Now accessing these preferences won't trigger additional queries $notifications = $user->preference('notifications'); $theme = $user->preference(UserPreference::Theme); ``` -------------------------------- ### Define Laravel Model Preference Validation Rules Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Demonstrates how to define validation rules for model preferences using Laravel's validation system. Implement the `preferenceRules()` method in your Eloquent model to specify rules for each preference, ensuring data integrity. ```PHP ['boolean'], 'theme' => [Rule::in(['dark', 'light', 'system'])], ]; } } ``` -------------------------------- ### Handle Preference Validation Errors Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Illustrates how to catch `PreferenceValidationException` when attempting to set an invalid preference value. The exception's `errors()` method provides access to the specific validation messages. ```PHP setPreference('theme', 'invalid-theme'); } catch (PreferenceValidationException $e) { $errors = $e->errors(); // Handle the validation errors, e.g., display them to the user } ``` -------------------------------- ### Deleting Preferences (Individual and Multiple) Source: https://github.com/hosmelq/laravel-model-preferences/blob/main/README.md Explains how to remove preferences from a model. It covers deleting a single preference by key and deleting multiple preferences by providing an array of keys, including support for enum keys. ```php $user->deletePreference('notifications'); $user->deletePreference(UserPreference::Theme); // Using enum $user->deletePreferences([ 'notifications', UserPreference::Theme // Using enum ]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.