Download the PHP package amarenkov/laravel-mutable-content without Composer

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

amarenkov/laravel-mutable-content

tests Packagist coverage

Custom fields for Eloquent models, with an audit log written by database triggers. Adding a field needs no migration: values live in a single JSON column (jsonb on PostgreSQL), and field definitions come from two sources: PHP attributes in code and records in reference tables that an administrator edits by hand.

One field definition feeds everything at once: Eloquent, validation rules, the admin panel (-filament), the user-facing screens (-daisyui) and the OpenAPI docs (-scramble). Add a field, and it shows up in forms, tables, validation and API docs.

At a glance

The change log, written by database triggers, now holds both saves:

Screenshots

These screens are not part of this package. It has no user interface of its own: the screenshots show the companion packages -filament and -daisyui on a demo catalog, with forms and tables built from the field definitions above.

Filament admin panel (laravel-mutable-content-filament): the table and the form come from the field definitions; "Gears" and "Color" were added in the admin panel.

Bicycles in the Filament admin panel

daisyUI screens (laravel-mutable-content-daisyui): the same model in a server-rendered Blade and Livewire application.

Editing a bicycle on the daisyUI screens

What it solves

  1. Users create and change object fields. New fields are added to a class in the admin panel, without migrations or a release.
  2. Users create and change lists of values (LOV). Statuses, categories and other reference lists are maintained in the admin panel, next to the system lists declared in code.
  3. Every content change is logged. The log is written by database triggers, not by the application, so changes made around it, even with plain SQL, still leave a trace with the old and new values.
  4. Code sets the minimum, users extend it. Fields and lists declared in code cannot be deleted in the admin panel, and their "required" and "immutable" flags can only be switched on there, not off.
  5. Data is not lost on write. The fields column cannot be overwritten as a whole, keys that are not declared as fields are kept, and concurrent saves of different fields do not overwrite each other.
  6. Who and why. Every change in the log carries its author, comment of any length and any other structured data, and an event without field changes can be logged too (logEvent()).

Features

Requirements

On MariaDB a table name with a schema, such as projects.projects, becomes projects__projects in one database: models, Schema::createWithLog() and LogHelper translate it, so the same models and migrations work on both. Schema::createSchema() does nothing there. Use the model's getTable() instead of a literal name with a schema in raw queries. Log dates are set by the database, so set the connection timezone to the application timezone on MariaDB (on PostgreSQL the log date is timestamptz).

SQLite and MySQL are not supported: the change log needs triggers and JSON functions of PostgreSQL or MariaDB. Run the test suite of your application against one of them too, for example a PostgreSQL service in CI, rather than an in-memory SQLite database.

Installation

The command publishes the migrations, migrates and seeds the system lists of values and fields (--config publishes the config file too, --no-migrate only publishes the migrations). The same by hand:

Run the seeders in this order: lists of values first, then fields. Run them again on deploy: they add the fields and lists declared in code since the last run. An item removed from a list in code stops being system and can be deleted in the admin panel; the seeder does not delete it, as records may still refer to it.

Quick start

A model:

Register it in your service provider:

A migration:

To roll it back, use Schema::dropIfExistsWithLog('projects.projects'): a plain Schema::drop() leaves the log table and its functions behind.

Usage:

Fields read like attributes but are not columns: query them with whereField() (see Querying fields, where('priority', 10) is an SQL error), and toArray() returns them under fields, not at the top level. Columns made by fieldExtract() are real columns.

Validation rules are built from the same definition:

Type settings, such as link_by_code and allow_unlisted_codes of an object field, are set in the admin panel or in code with the TypeSettings attribute:

A field shared by several classes has one label, but each usage may have a label of its own that replaces it in that class (forms, tables, validation messages, API docs). Set it in the admin panel or in code with the UsageLabel attribute; FieldsSeeder copies it into an empty usage label:

Querying fields

whereField() and orWhereField() compare a field by its type. A plain where('fields->weight', '>', 10) compares JSON values as text on PostgreSQL, so 9.5 is greater than 10 there; whereField() casts numbers and measurements, takes value objects in base units and dates as Y-m-d:

A field extracted to a column with fieldExtract() is compared on that column, so its index is used. The operators are =, <>, !=, <, <=, > and >=.

Change log

Database triggers write every insert, update and delete of a table created by Schema::createWithLog() to its log table: the action (created, updated, deleted, restored, force_deleted, or event for logEvent()), the fields before and after, the log data and the id of the database transaction, so entries of one operation can be grouped.

The log data is one JSON object: the author (user_id), the comment (comment) and any other keys of the application. The user_id column is generated from it for filtering by author. The data reaches the triggers through the database session, not through columns of the table:

Data of a nested context is merged over the outer one by top-level keys. The default context is cleared before every queued job (not on the sync connection) and Octane request.

Plain SQL outside the application sets the context itself, in the same transaction on PostgreSQL:

A change without a context is still logged, without an author. On MariaDB the transaction id is filled only for writes inside a context.

Events without field changes take a translation key or a text, stored in the data as event and translated when the log is shown, with scalar values of the data as replacements. An event takes the pending withLogContext() of the model too and leaves it for the next save:

The triggers write the full fields before and after. Compression replaces them with the changed fields only. The package schedules mutable-content:compress-log every minute without overlapping, so the application only needs the Laravel scheduler running (schedule:run in cron). Each run processes up to --limit entries of each class, and parallel runs skip each other's entries. Deletions, restorations and events get empty changes; an update without changes is left uncompressed with the error compression status and a warning in the application log, for review. Compression never retries such entries; after the review, return them with UPDATE <log table> SET compression_status = 'pending' WHERE compression_status = 'error'.

The schedule and the limit are set in the config: MUTABLE_CONTENT_LOG_COMPRESS_SCHEDULE, a cron expression, empty to disable, and MUTABLE_CONTENT_LOG_COMPRESS_LIMIT. Publish it with php artisan vendor:publish --tag=mutable-content-config.

Deleting old entries is up to the application: mutable-content:prune-log is never scheduled by the package. To delete entries older than three years every night:

Both commands take --class= to limit them to some classes (all registered classes by default), and the same is available as LogHelper::compress() and LogHelper::prune().

Adding to an existing table

Fields and the change log work separately and on any table with a single-column primary key, integer or not. Schema::createWithLog() and ModelWithFields are a shorthand for the case of a new table with everything on.

Use either part alone: the triggers need no trait, and HasMutableFields needs no log. SoftDeletes is optional; without it uniqueFieldSets() restores nothing and the log has no deleted and restored entries. Only a model with MutableModel can be added to MutableClassRegistry; mutable-content:compress-log and mutable-content:prune-log take any other model through --class.

The history starts at addLog(). Values of the listed columns are logged next to the fields, as the database renders them, so dates and booleans look different on PostgreSQL and MariaDB, and on MariaDB a JSON column is logged as a string. A field code cannot be the name of a plain column of the table.

The triggers are built by the columns the table has at that moment. After changing the key, adding fields or deleted_at, or dropping a logged column, run Schema::refreshLog('customers'); pass columns: to change the list. Schema::dropLog('customers') removes the log and keeps the table. A class overriding a method of HasMutableFields or HasChangeLog, such as save() or getAttribute(), replaces it, as PHP does with any trait: import the method under an alias and call it.

Moving definitions between environments

Fields, their usages, lists of values and items created in the admin panel live in the database. Move them, for example from staging to production, as JSON:

The export holds only records created in the admin panel: system ones come from the code and the seeders. References between records are written as codes, and the import matches records by codes: it creates the missing ones and updates the keys present in the file, never touches system records and runs in one transaction. Every change lands in the change log with the comment. DefinitionsHelper::export() and DefinitionsHelper::import() do the same in code.

Translations

Labels in attributes are passed through __() when read, so they can be translation keys or plain text. The package labels are in English and translated in lang/ru.json; messages are in lang/{en,ru}/*.php under the mutable-content namespace. Publish them to override:

Labels from code are written to the database by the seeders in the current locale; after that the database label wins and is edited in the admin panel.

Caveats

Compared to other packages

The trade-off: PostgreSQL or MariaDB only, PHP 8.4 and Laravel 13.

Companion packages

License

MIT. See LICENSE.


All versions of laravel-mutable-content with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
brick/math Version ^0.14.2 || ^0.15 || ^0.16 || ^0.17 || ^0.18 || ^0.19 || ^0.20 || ^1.0
laravel/framework Version ^13.8
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 amarenkov/laravel-mutable-content contains the following files

Loading the files please wait ...