Adds a flexible key/value table to your Laravel models.
I often find myself needing to store extra bits of information across entities in my apps. This could be a github link, maybe a SEO title or a path to a file.
Instead of expanding my database with new fields across multiple entities, I often resort back to a simple key value table
that has a polymorphic relationship
to any entity.
This package provides you with that table and a trait
that you can add to your models to implement the functionality.
To install the package, use Composer:
composer require digitalruby/meta-data
After installing, you should run the migration to create the meta_data
table in your database:
php artisan migrate
This command creates the necessary table for storing meta data related to your models.
-
Add the Trait to Your Model:
First, include the
HasMetaData
trait in any Eloquent model you wish to associate meta data with.use DigitalRuby\MetaData\HasMetaData; class YourModel extends Model { use HasMetaData; // Model content }
-
Setting Meta Data:
To add or update meta data for a model, use the
setMeta
method.$model->setMeta('key', 'value');
key
: The meta data key.value
: The meta data value. Can be a string, array, or object. Arrays and objects are automatically encoded to JSON.
-
Getting Meta Data:
Retrieve a value with the
getMeta
method.$value = $model->getMeta('key');
Optionally, you can retrieve all meta data associated with the model:
$allMeta = $model->getAllMeta();
-
Deleting Meta Data:
To remove a meta data entry, use
deleteMeta
.$model->deleteMeta('key');
This will delete the meta data entry with the specified key.
-
Advanced Usage:
The
getMeta
method supports additional query modifications via a callback function, allowing for more complex queries.
- PHP 8 or higher
- Laravel 10 or higher
We welcome contributions! Please submit pull requests for bug fixes, features, or improvements.
The DigitalRuby/MetaData package is open-sourced software licensed under the MIT license.