Download the PHP package klaude/eloquent-preferences without Composer
On this page you can find all versions of the php package klaude/eloquent-preferences. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download klaude/eloquent-preferences
More information about klaude/eloquent-preferences
Files in klaude/eloquent-preferences
Package eloquent-preferences
Short Description Preferences for Laravel Eloquent models
License MIT
Homepage https://github.com/klaude/eloquent-preferences
Informations about the package eloquent-preferences
Preferences for Laravel Eloquent models
Use this library to bind multiple key/value pair preferences to your application's Eloquent models. Preferences are stored in your application's database so they can be easily stored and queried for. This library supports Eloquent 5 through 8 installed either standalone or as a part of the full Laravel framework. Issues and pull requests are welcome! See CONTRIBUTING.md for more information.
- Installation
- Configuring In Laravel
- Configuring Without Laravel
- Usage
- Helper Methods
- Retrieving Preferences
- Setting Preferences
- Removing Preferences
- Default Preference Values
- Casting Preference Values
- Hidden Preference Attributes
Installation
Run composer require klaude/eloquent-preferences
to download and install the library.
Configuring In Laravel
1) Add EloquentPreferencesServiceProvider
to config/app.php
:
2) Install the configuration and database migration files:
3) Model preferences are stored in the "model_preferences" database table by default. If you would like to use a different table then edit the "table" entry in config/eloquent-preferences.php
.
4) Install the model preferences database:
Configuring Without Laravel
1) Model preferences are stored in the "model_preferences" database table by default. If you would like to use a different table then define the MODEL_PREFERENCE_TABLE
constant at your project's point of entry with your preferred table name.
2) Install the model preferences database. There are a number of ways to do this outside of Laravel. Here's the schema blueprint to apply:
Usage
Add the HasPreferences
trait to the Eloquent models that you would like to have related preferences.
This builds a polymorphic has-many relationship called "preferences" that you can query on your model like any other Eloquent relationship. Model preferences are modeled in the KLaude\EloquentPreferences\Preference
class. A preference object has preference
, value
, and Eloquent's built-in created_at
and updated_at
attributes. The HasPreferences
trait can be used by any number of model classes in your application.
Eloquent queries can be run directly on the Preference
class as well.
Helper Methods
The HasPreferences
trait has a number of helper methods to make preference management a little easier.
Retrieving Preferences
Call the getPreference($preferenceName)
or prefers($preferenceName)
methods to retrieve that preference's value.
Setting Preferences
Call the setPreference($name, $value)
or setPreferences($arrayOfNamesAndValues)
methods to set your model's preference values. Setting a preference either creates a new preference row if the preference doesn't exist or updates the existing preference with the new value.
Removing Preferences
Call the clearPreference($preferenceName)
, clearPreferences($arrayOfPreferenceNames)
, or clearAllPreferences()
methods to remove one, many, or all preferences from a model. Clearing preferences removes their associated rows from the preferences table.
Default Preference Values
By default, getPreference()
and prefers()
return null
if the preference is not stored in the database. There are two ways to declare default preference values:
1) Use an optional second parameter to getPreference()
and prefers()
to define a default value per call. If the preference is not stored in the database then the default value is returned.
2) Avoid requiring extra parameters to every getPreference()
and prefers()
call by declaring a protected $preference_defaults
array in your model containing a key/value pair of preference names and their default values. If the preference is not stored in the database but is defined in $preference_defaults
then the value in $preference_defaults
is returned. If neither of these exist then optional default value parameter or null
is returned.
Please note default preference values only apply when using the getPreference()
and prefers()
methods. Default values are not honored when retrieving preferences by Eloquent query.
Casting Preference Values
Preferences are stored as strings in the database, but can be cast to different types when retrieved.
Declare a protected $preference_casts
array in your model containing a key/value pair of preference names and the types to cast their values to. Preferences are stored and cast according to the same rules as Eloquent's attribute type casts.
As with default values, casting preferences is only performed when using the getPreference()
, prefers()
, setPreference()
, and setPreferences()
helper methods.
Hidden Preference Attributes
By default all preference model attributes are visible when exporting to JSON. However it is possible to declare hidden attributes that act in the same manner as Eloquent's hidden attributes. There are two ways to declare which preference attributes to hide from JSON export:
1) If this library is being used in a Laravel project then declare hidden attributes in the "hidden-attributes" key in config/eloquent-preferences.php
.
2) If this library is being used outside the Laravel framework then define the MODEL_PREFERENCE_HIDDEN_ATTRIBUTES
constant at your project's point of entry with a comma-separated list of attributes to hide from JSON export.
All versions of eloquent-preferences with dependencies
illuminate/database Version ~5|~6.0|~7.0|~8.0|~9.0|~10.0|~11.0
illuminate/support Version ~5|~6.0|~7.0|~8.0|~9.0|~10.0|~11.0