Download the PHP package hosmelq/laravel-model-preferences without Composer

On this page you can find all versions of the php package hosmelq/laravel-model-preferences. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-preferences

Laravel Model Preferences

Laravel Model Preferences provides a simple, fluent API for storing per-model preferences. You may store preferences in a JSON column or database tables and define defaults and validation rules per model.

Table of Contents

Introduction

Model preferences are small settings that live alongside a model, such as themes, notification options, or feature toggles. This package provides a clean API to read and write preferences with sensible defaults and validation.

Installation

First, install the package via Composer:

Next, publish the configuration and migration files:

If you prefer, you may publish the assets manually:

Finally, run your migrations if you are using the shared or table stores:

Configuration

After publishing the package assets, the configuration file will be located at config/model-preferences.php. This configuration file allows you to specify the default store and configure each store.

Default Store

You may specify the default preference store using the model-preferences.default configuration value or the MODEL_PREFERENCES_DRIVER environment variable. Supported drivers are column, shared, and table.

Store Configuration

Each store has its own configuration. For example, you may configure the JSON column name for the column driver or the database connection and table name for the shared and table drivers.

Environment Variables

The following environment variables are supported:

Defining Preferences

The InteractsWithPreferences Trait

To define preferences on a model, add the InteractsWithPreferences trait and implement the HasPreferences contract:

The preferencesConfig Method

The preferencesConfig method allows you to define defaults, validation rules, and the driver for a given model:

Defaults

Defaults may be defined per model using withDefaults. These values will be returned when the preference key is not present in the store:

Validation Rules

You may validate preferences using Laravel's validation rules. Rules are evaluated when calling set, and a PreferenceValidationException will be thrown if validation fails:

Using Preferences

Obtaining a Preferences Instance

Call the preferences method on your model to obtain a scoped preferences instance:

Retrieving Preferences

Use get to retrieve a single preference value. You may pass a default value or a closure that will be evaluated lazily. To retrieve multiple preferences at once, use getMultiple:

Storing Preferences

Use set to store a preference value or setMultiple to store multiple values:

Checking for Presence

Use has to determine if a preference key exists. You may also use missing to check the opposite:

Retrieving All Preferences

Use all to retrieve all stored preferences:

Defaults & Missing Values

Preference lookup follows these rules:

Deleting Preferences

Use delete to remove a single preference, deleteMultiple for batches, and clear to remove all preferences:

Enum Keys

Preference keys may be strings, backed enums, or unit enums. Backed enums use their backed value, while unit enums use their name:

Preference Stores

You may select a store globally via configuration or per model using withDriver.

Column Driver (JSON Column)

The column driver stores all preferences in a single JSON column on the model's table. You may configure the column name via withColumn or the model-preferences.stores.column.name setting. When using this driver, the InteractsWithPreferences trait automatically adds a JSON cast for the configured column.

Example migration column:

Shared Table Driver (Polymorphic)

The shared driver stores preferences in a single table using a polymorphic relation. The published migration creates a preferences table with preferable_type, preferable_id, key, and value columns.

Per-Model Table Driver

The table driver stores preferences in a dedicated table per model. When using this driver, you must configure the table name in preferencesConfig:

Custom Tables & the Artisan Generator

You may generate a preferences table migration using the Artisan command:

This stub creates a table with model_id, key, and value columns and a unique index on model_id and key.

Adding Custom Preference Drivers

Implementing a Driver

To add your own driver, implement the PreferenceDriver contract. If you need to distinguish between missing values and null, implement PresenceAwarePreferenceDriver and return a PreferenceRead instance from getWithPresence.

Registering a Driver

Register your driver via the preferences manager, then configure your model to use it:

Model Cleanup

When using the shared or table drivers, the InteractsWithPreferences trait automatically removes stored preferences when the model is deleted.

Testing

Changelog

See CHANGELOG.md for a list of changes.

Contributing

Pull requests are welcome. Please run the test suite before submitting changes.

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-model-preferences with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/contracts Version ^12.0 || ^13.0
illuminate/database Version ^12.0 || ^13.0
illuminate/support Version ^12.0 || ^13.0
spatie/laravel-package-tools Version ^1.92
thecodingmachine/safe Version ^3.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package hosmelq/laravel-model-preferences contains the following files

Loading the files please wait ...