Download the PHP package hyvor/laravel-json-meta without Composer

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

Laravel JSON Meta

A library for saving metadata in a JSON column, with strict type checking using PHPStan.

Installation

Install the package via composer.

If you use PHPStan (highly recommended), add the following to your phpstan.neon file. This improves the type checking of the library.

Metadata or column?

When saving data, you have to decide between meta vs column. A general rule is that if that data is needed for a WHERE or ORDER BY, save it in a column. If not, you may save it in metadata. In most cases, configuration options are the best things to save in metadata.

(You can still use metadata in WHERE queries if your database engine supports JSON operations.)

Usage

Let's say you want to save metadata of blogs in a JSON column named meta.

First, add a JSON meta column to the table.

If your database does not support native JSON columns, Laravel will create a TEXT column, which works fine with this library.

Update the Model

Definition

"Definition" is which data types you allow saving in the meta field. By defining them, you can make sure that incorrect data is never inserted by invalid user input or typos in your code. The following methods are available in the MetaDefinition class.

Default value

You should always set a default value for each metadata.

Use the default method to set the default value. The value should be of the same type as the type you defined.

Or, you can use nullable to set the default value to null.

In the above example, the type of seo_robots is now string|null.

Methods

The HasMeta trait adds the following methods to the model.

metaGet(string $name) : mixed

To get a meta value by name. The $name should be one of the names you defined in the metaDefinition function. If the value is not set, the default value will be returned.

metaGetAll() : array

To get all meta values. All keys you define in metaDefinition function will be set in the returned array, filled with default values if missing in the meta field.

metaSet(string|array $name, mixed $value) : void

Set meta $name to $value. In addition to static type checking via PHPStan, runtime type checking is done here to prevent invalid values from being saved.

You may also send an array as the first param to update multiple values.

Types

Include the PHPStan extension to get better type checking.

This registers the generic utility type meta-of<Model>. You can use this type to easily get the type of the meta fields as a constant array.

Why?

Why save metadata in a JSON column?

Let's see other options:

CASE 1: You could save metadata in separate columns.

CASE 2: You could save metadata in a separate table.

This is actually a good option. Check the laravel-meta library which has a similar concept like this library but saves data in a separate table.

How data is saved

When a new model is created, meta is NULL. Meta column only contains fields that were changed. For example, if you set seo_indexing to false, you will have the following JSON in the meta column:

This way, you will save a lot of space in the database.

Adding new metadata

Let's say you want to add a new metadata called seo_follow_external_links. This task is pretty easy. All you have to do is adding this to the definition.

Updating metadata

Updating is a bit tricky. Let's say you want to rename seo_indexing to seo_indexing_on. You could update the definition, but the problem is with the values that are already saved in the database.

How do you update this:

to this:

One option is to use JSON operations in the database. Or, use a custom script. Currently, this library does not provide this feature out of the box. But, if required in the future, we will add a command to do this. (Contributions/ideas are welcome)

Removing metadata

If you need to remove seo_indexing option from your application, you should first remove it in the meta definition.

You will still have old data saved in the database meta fields, but it should not be a problem as that data will be not be used even its there.

However, if you are required to delete those data (ex: for legal reasons), you will need an option similar to the "Updating metadata" section. Again, contributions are welcome.


All versions of laravel-json-meta with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3|^8.2
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 hyvor/laravel-json-meta contains the following files

Loading the files please wait ....