miladev / lara-meta
Save meta with any model.
Requires
- php: ^7.3|^8
- illuminate/database: ^7|^8|^9
- illuminate/support: ^7|^8|^9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- orchestra/testbench: ^7.5
- phpunit/phpunit: ^9.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-08 08:54:30 UTC
README
Save metadata (key, value) with any model.
Sometimes, we may need to store few extra information for some objects. In some situation, it's not good solution to add new columns. This package can solve those issues.
The package will create a table in database named laravel_metas with key, value and metable column.
However, table name can be changed by updating table_name in config/meta.php.
N.B: After changing table_name, you need to delete the previous table (if exists) from DB and delete the create_meta_table row from migrations table.
Then re-run the php artisan migrate command again.
Installation
You can install the package via composer:
composer require miladev/lara-meta
If you are using Laravel Package Auto-Discovery, you don't need you to manually add the ServiceProvider.
Without auto-discovery:
If you don't use auto-discovery, add the below ServiceProvider to the $providers array in config/app.php file.
Miladev\LaravelMeta\MetaServiceProvider::class,
If you want to change the meta table name, then first publish the config file.
php artisan vendor:publish --provider="Miladev\LaravelMeta\MetaServiceProvider"
Then, update the table_name value in config/meta.php.
Then you can run migration command to create database table.
php artisan migrate
Usage
Add Miladev\LaravelMeta\Metable trait to models where you need.
use \Illuminate\Database\Eloquent\Model; use \Miladev\LaravelMeta\Metable; class Post extends Model { use Metable; }
Then you can access like below:
$post = Post::withMetas()->first();
$post = Post::first(); $post->metas;
$post = Post::first(); $post->saveMeta('meta_key_here', 'value_here'); $post->getMeta('meta_key_here', 'default_value'); $post->updateMeta('meta_key_here', 'value_here_new'); $post->deleteMeta('meta_key_here'); $post->findMeta('value_here');
Typed Meta Values
Define a per-key cast on your model to automatically cast values on write and read. Supported casts: string (default), int/integer, float/double, bool/boolean, json, date, datetime.
class Post extends Model { use Metable; protected $metaCasts = [ 'views' => 'int', 'rating' => 'float', 'active' => 'bool', 'data' => 'json', 'published_on' => 'date', ]; }
$post->saveMeta('views', '100'); $post->getMeta('views'); // 100 (int) $post->saveMeta('active', true); $post->getMeta('active'); // true (bool) $post->saveMeta('data', ['a' => 1]); $post->getMeta('data'); // ['a' => 1] (array) $post->saveMeta('published_on', Carbon::parse('2024-01-15')); $post->getMeta('published_on'); // DateTime object
Roadmap
Ideas and planned features for future versions. Contributions are welcome!
- Typed meta values — cast values to
int,float,bool,json,date, etc. via a per-key cast definition. - Array/object serialization — automatically serialize arrays and objects (JSON) on save and unserialize on read.
- Set multiple metas at once —
saveManyMeta(['key' => 'value', ...])/updateManyMeta()in a single transaction. - Get all metas as array/collection —
getAllMetas(): arrayreturning a key/value map. - Sync metas —
syncMetas(array $metas)that creates, updates, and deletes keys to match the given array (like Laravel'ssync()). - Has-meta scopes —
scopeWhereHasMeta($query, $key)andscopeWhereMeta($query, $key, $value)to filter models by meta existence/value. - Meta value casting per model — override casts on the model, e.g.
protected $metaCasts = ['views' => 'int'];. - Numeric operators in scopes —
whereMetaGreaterThan(),whereMetaLessThan(),whereMetaIn(),whereMetaBetween(),whereMetaLike(). - Meta encryption — transparently encrypt/decrypt sensitive meta values (e.g.
encryptedcast). - Automatic model cleanup — delete related metas automatically when the metable model is deleted (cascade via model events or FK constraint).
- Global meta helper —
meta($model, 'key')andmeta_set($model, 'key', 'value')helper functions. - Artisan commands —
php artisan meta:set,meta:get,meta:delete,meta:listfor CLI inspection and management. - Tagged/grouped metas — an optional
groupcolumn so metas can be organized and fetched by group. - Meta with expiration — optional
expires_atcolumn and auto-cleanup of expired metas. - Revision/history tracking — keep a history of meta changes with old/new values, actor, and timestamps.
- Caching layer — cache loaded metas per model to avoid repeated queries, with automatic invalidation on write.
- Import/export — export all metas of a model (or the whole table) to JSON/CSV and re-import them.
- Counter metas —
incrementMeta($key, $amount)/decrementMeta($key, $amount)with atomic DB updates. - Batch operations — bulk set/get metas for collections of models, avoiding N+1 queries.
- Laravel Nova / Filament support — official fields/packages to view and edit metas from the admin panels.
Contribute
If you want to contribute, open a pull request by following Laravel contribution guide.
License
The MIT License (MIT). Please see License File for more information.