Download the PHP package thinktomorrow/dynamic-attributes without Composer
On this page you can find all versions of the php package thinktomorrow/dynamic-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thinktomorrow/dynamic-attributes
More information about thinktomorrow/dynamic-attributes
Files in thinktomorrow/dynamic-attributes
Package dynamic-attributes
Short Description easily store and retrieve document values of an eloquent model
License MIT
Informations about the package dynamic-attributes
Dynamic attributes
This small package adds nosql-like storage behaviour to your eloquent model. It allows the model to interact with json column values as if they were top-level attributes.
Installation
You can install the package via composer:
Setup
Here's the quick how to on setting up the package. We'll assume you already have an eloquent model set up.
- Add a json column to the database scheme of your eloquent model. By default, it should be named
values
. - Add the
HasDynamicAttributes
trait to your eloquent model. - Add a
dynamicKeys
property on the model and fill it with the value keys that should be stored and fetched from the json column.
Here's an example setup:
A json column
Behind the scenes the values are automatically transposed to and from the json attribute. On the model, no custom eloquent cast is needed, because the trait itself will handle the conversion to and from the stored value.
The dynamic attributes are preferably stored as a json database column but a string typed column should also be fine. The json column will just make your life easier when you need to query the database.
By default, the database column name is assumed to be values
. You are free to change this.
Usage
Setting a value: setDynamic(string $key, $value)
The value can be set just like you're used to with any other eloquent attribute. There's a setDynamic($key, $value)
method which allows you to explicitly set a dynamic attribute.
Getting a value: dynamic(string $key)
The value can be retrieved like a regular attribute. You can use the dynamic($key)
method to retrieve the dynamic attribute value as well.
Checking a value: isDynamic()
Checks if the passed key refers to a dynamic attribute key or not.
Raw values
You can get the raw array of dynamic values via the rawDynamicValues
method.
Localization
Dynamic attributes are built with localization in mind.
Setting a localized value is done by passing the locale as a nested key:
Retrieve the localized value:
An extra thing you can do is to add a dynamicLocales
property on your model. This is an array and should contain all the locales of the model. This ensures you return an null value in case a localized value isn't found instead of the entire value array itself.
Changing the database column name
By default the column name is set to values
. You can change this by overriding the dynamicDocumentKey
in your model and returning your custom column/attribute name.
Eloquent method inheritance
The trait class overrides some eloquent methods. These methods are: getAttribute
, setAttribute
, setRawAttributes
and removeTableFromKey
.
This is because of the integral connection with eloquent attribute logic and the way eloquent allows for behavioural change via inheritance.
If you use another trait that also overrides one of these methods, you're gonna bump into a method collision and need to alias the trait methods.
Other solutions
There's another great package that provides a similar functionality and that's spatie/laravel-schemaless-attributes. It also provides a nosql-like behaviour for json columns but takes a different approach. The main difference is that our package allows to work with top-level attributes and localized values, which was a requirement for some of our projects. With the package from Spatie, on the other hand, you can add multiple 'nosql' columns, which is currently not possible with our package.
Testing
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of dynamic-attributes with dependencies
ext-json Version *
illuminate/database Version ^8.12|^9.0|^10.0
doctrine/dbal Version ^3.1