waadmawlood/
laravel-model-metadata
g4t.io

Manage Metadata on Laravel Eloquent Models with JSON Support

16 views 87 stars by Paul Redmond

A Laravel package designed to manage model metadata with JSON support multiple data types

json laravel metadata package
README from waadmawlood/laravel-model-metadata Open on GitHub →

Laravel Model Metadata

Laravel Model Metadata

Laravel Model Metadata is a package designed to manage metadata with JSON support for multiple data types. It allows you to easily attach, manage, and query metadata on your Laravel models using the HasManyMetadata or HasOneMetadata traits.

📚 Documentation

For detailed documentation, including usage examples and best practices, please refer to the Documentation.

✨ Requirements

  • PHP 8.0 or higher
  • Laravel framework 9.30.1 or higher
  • JSON extension enabled

💼 Installation

  1. Install the package using Composer:

    composer require waad/laravel-model-metadata
    
  2. (Optional) Publish the config file first:

    php artisan vendor:publish --tag=metadata-config
    
  3. (Recommended) Clear the configuration cache to ensure new config is loaded:

    php artisan config:clear
    
  4. Publish the migration files:

    php artisan vendor:publish --tag=metadata-migrations
    
  5. Run the migrations:

    php artisan migrate
    

⚙️ Configuration

You can customize the metadata table name, model, and caching behavior by editing the published config file at config/model-metadata.php:

return [
    'table' => 'model_metadata',
    'model' => Waad\Metadata\Models\Metadata::class,

    'cache' => [
        'enabled' => env('MODEL_METADATA_CACHE_ENABLED', false),
        'ttl'     => env('MODEL_METADATA_CACHE_TTL', 3600),
        'store'   => env('MODEL_METADATA_CACHE_STORE', null),
        'prefix'  => env('MODEL_METADATA_CACHE_PREFIX', 'model_metadata'),
    ],
];

🎈 Usage

🔥 HasOneMetadata Trait

Add the HasOneMetadata trait to your model to enable a single metadata record:

use Waad\Metadata\Traits\HasOneMetadata;

class Company extends Model
{
    use HasOneMetadata;  // <--- Add this trait to your model
}

Some methods:

// Create metadata with array (only works if no metadata exists)
$company->createMetadata(['key' => 'value', 'another_key' => 'another_value']);

// Create metadata with collection
$company->createMetadata(collect(['key' => 'value']));

// Update existing metadata
$company->updateMetadata(['new_key' => 'new_value']);

// Delete the metadata
$company->deleteMetadata();

// Get metadata as array
$metadata = $company->getMetadata();

// Get metadata as collection
$metadataCollection = $company->getMetadataCollection();

🔥 HasManyMetadata Trait

Add the HasManyMetadata trait to your model to enable multiple metadata records:

use Waad\Metadata\Traits\HasManyMetadata;

class Post extends Model
{
    use HasManyMetadata;  // <--- Add this trait to your model

    // Enabled Append id with content metadata (default)
    public $metadataNameIdEnabled = true;

    // Custom Append key of id with metadata (default)
    public $metadataNameId = 'id';
}

see Configuration Append Id for more details

Some methods:

// Create metadata with array or collection
$post->createMetadata(['key1' => 'value1', 'key2' => 'value2']);
$post->createMetadata(collect(['key1' => 'value1', 'key2' => 'value2']));

// Update metadata by ID
$post->updateMetadata('{metadata_id}', ['new_key' => 'new_value']);

// Delete metadata by ID
$post->deleteMetadata('{metadata_id}');

// Get all metadata objects
$metadata = $post->metadata;
// or
$metadata = $post->metadata()->get();

// Get metadata by ID
$metadata = $post->getMetadataById('metadata_id');

// Get all metadata column pluck as array
$allMetadata = $post->getMetadata();

// Get all metadata column pluck as collection
$metadataCollection = $post->getMetadataCollection();

// Search in metadata
$searchResults = $post->searchMetadata('search_term');

🗄️ Cache

The package includes an optional caching layer to reduce database queries. Cache is disabled by default and can be enabled in the config:

You can configure cache settings either in config/model-metadata.php or override them via your .env file.

Config Key / Env Variable Type Default Description
cache.enabled / MODEL_METADATA_CACHE_ENABLED bool false Enable or disable metadata caching
cache.ttl / MODEL_METADATA_CACHE_TTL int 3600 Cache time-to-live in seconds
cache.store / MODEL_METADATA_CACHE_STORE string|null null (empty) Cache store to use (null = default Laravel cache driver)
cache.prefix / MODEL_METADATA_CACHE_PREFIX string model_metadata Prefix for all metadata cache keys

For example, to enable cache and customize settings, add to .env:

MODEL_METADATA_CACHE_ENABLED=true
MODEL_METADATA_CACHE_TTL=3600
MODEL_METADATA_CACHE_STORE=redis
MODEL_METADATA_CACHE_PREFIX=model_metadata

When caching is enabled:

  • Read operations (getMetadata, getMetadataById, getMetadataCollection) are automatically cached.
  • Write operations (create, update, delete, forget, sync) automatically invalidate the cache.
  • You can manually clear the cache for a specific model using clearMetadataCache():
$company->clearMetadataCache();
$post->clearMetadataCache();
  • You can check if caching is active:
$company->metadataCacheIsEnabled(); // bool

🧪 Testing

To run the tests for development, use the following command:

composer test

👨‍💻 Contributors

📝 License

This package is open-sourced software licensed under the MIT license.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News