nguyentranchung / laravel-metable
Fluent Meta Data for Eloquent Models, as if it is a property on your model.
dev-master
2018-11-19 14:07 UTC
Requires
- php: >=7.0.0
- illuminate/support: >=5.0
This package is auto-updated.
Last update: 2025-02-20 03:58:45 UTC
README
Metable Trait adds the ability to access meta data as if it is a property on your model. Metable is Fluent, just like using an eloquent model attribute you can set or unset metas. Follow along the documentation to find out more.
Installation
Composer
composer require nguyentranchung/laravel-metable
Migration
php artisan vendor:publish --provider=NguyenTranChung\Metable\MetableServiceProvider --tag=migrations
Configuration
php artisan vendor:publish --provider=NguyenTranChung\Metable\MetableServiceProvider --tag=config
Model Setup
use NguyenTranChung\Metable\Metable\Metable; use NguyenTranChung\Metable\Metable\MetableTrait; class Post extends Model implements Metable { // ... use MetableTrait; }
Usage
Add Meta
$post = Post::first(); // add single meta $post->setMeta('key0', 'value0'); $post->setMeta('key1', 'value1'); // add multi metas $post->setMeta([ 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4', ]); // auto save meta $post->save(); // or $post->updateOrCreateMetas();
Delete Meta
// unset single meta key $post->unsetMeta('key0'); // unset multi meta key $post->unsetMeta(['key1', 'key2', 'key3']); // auto save change meta $post->save(); // or $post->deleteMetas(); // Delete all metas $post->deleteAllMetas();
Get Meta
// get all metas $post->getMeta(); // get meta with key $post->getMeta('key1'); // get multi meta with keys $post->getMeta(['key1', 'key2', 'key3']); // Get value of key, only string key input $post->getMetaValue('key1');
Eager Loading
When you need to retrive multiple results from your model, you can eager load metas
$post = Post::with(['metas'])->get();