Download the PHP package tobymaxham/laravel-properties without Composer

On this page you can find all versions of the php package tobymaxham/laravel-properties. 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-properties

Laravel Properties

Latest Version on Packagist Tests Total Downloads Support me on Patreon

Flexible properties for your Eloquent models, stored in a single JSON column. With defaults, casts, enums, encryption, query scopes and migration helpers.

Contents

Requirements

Use version 1.x for older PHP or Laravel versions.

Upgrading from 1.x? Please read the AI agent do the upgrade.

Installation

The service provider is registered automatically.

Add a nullable JSON column to your table:

For an existing table, create the migration with Artisan:

Then add the trait to your model:

The trait adds the cast for the column itself, you do not need 'properties' => 'array'.

Implementing TobyMaxham\LaravelProperties\Contracts\HasProperties is optional. It lets other code detect models with properties.

Reading and writing

Keys use Laravel's "dot" notation. All methods change the model only, call save() to store the changes.

All setters return the model, so you can chain them: $user->setProperty('a', 1)->forgetProperty('b')->save().

A key with a dot ('a.b' as real key) can be read, but not written with the methods above. Avoid dots in keys.

The props() object

props() gives you fluent access to the same properties. It is handy in views, resources and forms.

props() has the same methods as the model: get, set, has, forget, all, stored, defaults, only, except, merge, replace, clear, push, pull, increment, decrement, toggle.

Configuration

Everything is optional. Use a method, a constant or a property, whatever you prefer (a method wins over a constant, a constant over a property).

What Method Constant Property Default
Column propertiesColumn() PROPERTIES_COLUMN $propertiesColumn 'properties'
Type of $model->properties propertiesCast() PROPERTIES_CAST $propertiesCast Array
Defaults propertyDefaults() PROPERTY_DEFAULTS $propertyDefaults []
Casts propertyCasts() PROPERTY_CASTS $propertyCasts []
Defaults in toArray() serializePropertyDefaults() SERIALIZE_PROPERTY_DEFAULTS $serializePropertyDefaults false

Type of the attribute

$model->properties (the attribute itself) returns the stored values, without defaults and property casts:

PropertiesCast $model->properties is Like Laravel's
Array (default) array array
Object ArrayObject, $model->properties['a'] = 1 works AsArrayObject
Collection Collection AsCollection
EncryptedArray, EncryptedObject, EncryptedCollection as above, the whole column is encrypted encrypted:array, ...

An existing Laravel cast of the column (array, json, AsArrayObject, encrypted:array, ...) is detected and keeps its type. A custom cast class is kept as it is.

Defaults

Defaults are only used when reading. They are never stored, so the model does not become dirty, and a changed default applies to all existing rows at once.

By default, toArray() and JSON only contain the stored properties. Let the model or a single call decide:

Casts

Cast single properties like Laravel's attribute casts. Keys may use "dot" notation.

Cast Reading returns Stored as
int / integer, float / double / real int, float number
decimal:<digits> string with the digits string, e.g. "12.50"
bool / boolean bool ('1', 'yes', 'on' → true) boolean
string string string
array / json, collection array, Collection JSON array / object
date, datetime, immutable_date, immutable_datetime, with optional :<format> Carbon (respects Date::use()) Y-m-d, Y-m-d H:i:s or the format
a backed enum class the enum the value
encrypted, encrypted:<cast> see Encryption encrypted string

The rules:

Enums are strict by default: an unknown value throws when reading or writing. Use EnumCast::lenient(Mood::class) to get null instead. Int backed enums also accept '2' from forms.

Custom casts implement TobyMaxham\LaravelProperties\Contracts\PropertyCast:

Use it as MoneyCast::class, MoneyCast::class.':USD' (arguments separated by ,) or new MoneyCast('USD') inside propertyCasts().

Encryption

Single properties are encrypted with the encrypted cast. Add a cast for the decrypted value after the colon:

The whole column is encrypted with an encrypted type. The column must be text:

Like Laravel's encrypted:array, toArray() contains the decrypted values of an encrypted column.

Encrypted values can not be queried: every encryption of the same value is different. The query scopes throw a CannotQueryEncryptedPropertyException. Only whereHasProperty() works for single encrypted properties. Check it yourself with $model->props()->isQueryable('api_token').

Queries

The scopes work on relations and inside whereHas(), and the column is qualified (users.properties), so joins work.

What the scopes do for you:

Good to know:

Migrations and indexes

Columns

Indexes

Add an index for properties you query often:

The index uses exactly the expression of the query scopes, so the database uses it:

Database Index
MySQL a virtual column properties_ui_theme with an index, used automatically by whereProperty()
PostgreSQL an expression index
SQLite an expression index
MariaDB a virtual column with an index. MariaDB does not use it automatically, query the column: where('properties_ui_theme', 'dark')
SQL Server not supported

Indexes help with =, in and string comparisons. Numeric comparisons (>, <, orderByProperty() of numbers) do not use them.

Changing existing data

Change the properties of all rows in a migration. It runs in chunks, without model events and without touching updated_at, and returns the number of changed rows:

Move columns into properties and back:

Database support

The package is tested on every change with:

Database Tested version JSON column Notes
SQLite 3 text invalid JSON is only rejected by the package
MySQL 8.4 json reorders the keys of objects, the data stays the same
MariaDB 11 longtext with check indexes need a direct query, see Indexes
PostgreSQL 17 json or jsonb
SQL Server (generated SQL) nvarchar(max) no property indexes

When to use properties

Good for: settings and preferences, feature flags, optional data that only some rows have, data with a structure that changes often, data from external systems.

Use real columns for: foreign keys, data you filter, sort or join by in large tables all the time, and data that needs constraints (unique, not null, foreign keys). With moveOut() you can turn a property into a column later.

Exceptions

All exceptions extend TobyMaxham\LaravelProperties\Exceptions\PropertiesException:

Exception When
InvalidPropertyValueException a value does not fit its cast, e.g. 'abc' for int, unknown enum value, incrementProperty() on a string
InvalidAttributeTypeException the column does not contain a JSON object or array
CannotQueryEncryptedPropertyException a query uses an encrypted property or an encrypted column

An invalid configuration (unknown cast, empty column name, default for an encrypted property, ...) throws an InvalidArgumentException.

Testing

Run the tests against another database:

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Support me

ko-fi
Support me on Patreon

Credits

License

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


All versions of laravel-properties with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/console Version ^12.0|^13.0
illuminate/database Version ^12.0|^13.0
illuminate/filesystem Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
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 tobymaxham/laravel-properties contains the following files

Loading the files please wait ...