jessegall/metadata

This package is abandoned and no longer maintained. No replacement package was suggested.

A package that enabled eloquent models to store metadata

This package has no released version yet, and little information is available.


README

A package that enabled eloquent models to store metadata

Installation

Using composer

composer require jessegall/metadata

Or manually by modifying the composer.json file:

{
    "require": {
        "jessegall/metadata": "^1.0"
    }
}

Run composer install

Add the service provider to config/app.php

'providers' => [
    // ...
    \JesseGall\Metadata\Providers\MetadataServiceProvider::class,
]

Run the following command to publish the config file

php artisan vendor:publish --provider="JesseGall\Metadata\Providers\MetadataServiceProvider"

Configure the config file (metadata.php) located in the config folder

return [
    /**
     * Set to true if you use uuids for your models
     */
    'uuid' => false,
];

Run the migrations command: php artisan:migrate

Usage

You can now add the HasMetaData trait to any eloquent model.

class Example extends Model
{
    use HasMetadata;
    ...
}

You can store all primitives types as value. Use dot notation for nested data. The metadata will be automatically be saved when the model is saved

$example = new Example();

$example->setMetadata('string', 'a string');
$example->setMetadata('array', ['key' => 'value']);
$example->setMetadata('int', 1);
$example->setMetadata('float', 1.1);
$example->setMetadata('nested.one', 'hello');
$example->setMetadata('nested.two', 'bye');
$example->setMetadata('nested.three', ['key' => 'value']);

$example->save(); // Don't forget to save the model

To retrieve metadata from the model:

$example->getMetadata('string'); // 'a string'
$example->getMetadata('array'); // ['key' => 'value']
$example->getMetadata('int'); // 1
$example->getMetadata('float'); // 1.1
$example->getMetadata('nested.one'); // 'hello'
$example->getMetadata('nested.two'); // 'bye'
$example->getMetadata('nested.three'); // ['key' => 'value']
$example->getMetadata('nested.three.key'); // 'value'
$example->getMetadata('nested'); // ['one' => 'hello', 'two' => 'bye, 'three' => ['key' => 'value']]

If you want the model to append the metadata when the toArray method is called. Add 'metadata' to the appends array:

class Example extends Model
{
    use HasMetadata;
    
    protected $appends = [
        'metadata',
        ...
    ];
    
    ...
}