Download the PHP package kyzegs/laravel-eloquent-embeddables without Composer

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

Laravel Eloquent Embeddables

Doctrine-like embeddables for Eloquent, built on Laravel's native custom cast system.

Group several plain database columns into a rich PHP value object — no JSON column, no extra table, and no trait required on the parent model.

…while the database keeps ordinary columns:

Mental model

Laravel custom casts, but powerful enough to behave like Doctrine embeddables.

An embeddable owns structure, casting, validation and clean object access. The parent model owns persistence. The embeddable has no table, no identity, and is never saved on its own — it is persisted only through its parent.

Requirements

Installation

Defining an embeddable

Extend EmbeddableModel. It reuses Eloquent's attribute machinery (fillable/guarded, casts, accessors, mutators, hidden, visible, appends, toArray()/toJson(), default attributes) but refuses every persistence and relationship method.

Using it in a parent model

Add the cast to the parent's casts() method. No trait is required.

Zero-config form

The prefix defaults to the cast key + _ (address_), and the columns are discovered by matching that prefix against the parent table's real columns (one cached schema query per table). No columns match, or no connection is available? The map falls back to the embeddable's fillable attributes and cast keys, prefixed.

The full resolution order, most explicit first:

  1. An explicit columns map.
  2. Explicit attributes, prefixed.
  3. Schema discovery: parent table columns matching the prefix.
  4. Fallback: the embeddable's fillable attributes + cast keys, prefixed.

If none of these produce a map, an InvalidArgumentException explains exactly what was tried. Combining columns with prefix/attributes throws as well — the forms are mutually exclusive.

Prefix form

Maps:

Explicit column-map form

Working with embeddables

Read like an object:

Assign an array:

Assign an instance:

Mutate in place — changes are synced back to the parent columns on save:

Compare as value objectsequals() is true for the same concrete class with identical attributes, regardless of order:

Nullable embeddables

With nullable: true:

When reading, if all mapped columns are null, the cast returns null instead of an empty embeddable.

Serialization

Use the optional HasEmbeddables trait on the parent to expose a clean nested object in toArray() / toJson(). It appends each embeddable and hides its backing columns:

Without the trait, casts/reads/writes still work — the model just serializes the flat columns instead of a nested object.

Querying

Embeddables are plain columns, so query the parent model normally:

IDE support (laravel-ide-helper)

barryvdh/laravel-ide-helper works out of the box: ide-helper:models resolves the cast through EmbeddableCast::get() and annotates every embeddable as the generic EmbeddableModel|null.

For the concrete class and correct nullability, register the shipped model hook in config/ide-helper.php:

ide-helper:models then generates:

The |null is only added when the cast is nullable. (The write side also accepts an attribute array or null; the generated single-type @property favors the concrete class.)

The hook also types the embeddable classes themselves. An embeddable has no table to introspect, so the hook finds a parent model embedding it (scanning ide-helper's model_locations), maps each attribute to its backing parent column, and derives the type from the embeddable's own cast or the column's schema type:

Make sure both the parent models and the embeddable classes live in directories covered by model_locations in config/ide-helper.php. When several parents embed the same class, their column maps are merged; the first parent to supply an attribute wins.

What is not supported

An embeddable is not a full Eloquent model. Every persistence method (save, saveOrFail, update, delete, forceDelete, push, refresh, fresh, touch, increment, decrement and their quiet variants) and every relationship method (hasOne, hasMany, hasOneThrough, hasManyThrough, through, belongsTo, belongsToMany, morphOne, morphMany, morphTo, morphToMany, morphedByMany) throws an EmbeddableException:

Embeddables are persisted through their parent Eloquent model. Call save() on the parent model instead.

Embeddables do not participate in relationships. Define the relationship on the parent Eloquent model instead.

How it works

EmbeddableCast::using() returns an encoded Castable definition, so Laravel's native cast resolver builds the configured cast. The attribute => column map is resolved lazily on first use, following the resolution order above. On the cast:

  1. get() reads the mapped parent columns and hydrates an embeddable (its own casts apply).
  2. set() flattens an array / instance / null back into the mapped columns.
  3. Because Eloquent re-invokes set() for cached cast objects during save(), direct mutations are synced to the parent automatically.
  4. serialize() renders the embeddable as a nested array for toArray()/toJson().

Testing

License

MIT.


All versions of laravel-eloquent-embeddables with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/contracts Version ^12.0|^13.0
illuminate/database 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 kyzegs/laravel-eloquent-embeddables contains the following files

Loading the files please wait ...