, (*1)
The Laravel Eloquent Metable Package is designed specifically for associating "meta" information to an Eloquent model., (*2)
Installation
Just place require new package for your laravel installation via composer.json, (*3)
composer require leemason/metable
Then hit composer update, (*4)
Compatability
The Metable package has been developed with Laravel 5.1, i see no reason why it wouldnt work with 5.0 or even 4 but it is only tested for 5.1., (*5)
Introduction
There are many use cases for this, most notably a User model where you may need the ability to assign multiple different types of profile information., (*6)
Ideally you would always provide unique columns on your model tables to handle all of the data, but in more dynamic applications this may not always be a viable option., (*7)
This is were "meta" information comes into its own., (*8)
This package provides the means to associate any kind of data to a model, from ints, float, bools, arrays, to Collections and objects., (*9)
Not only does it make it easy to assign this data, it also formats the data for the database and on return usage., (*10)
For example a User may need multiple social links, with this package you can create a collection with the data, save right there and then as a Collection (which gets saved as json encoded string).
Then whenever its retrieved in the future it will be converted back to a Collection., (*11)
The "meta" is saved as related Eloquent models with simple key/value access., (*12)
In the background the meta model also saved the values "type" for use when returning., (*13)
The package comes with 2 traits which adds all the functionality needed and a few helpers to make managing the information even easier., (*14)
The Metable trait is used to turn an Eloquent model into a meta model and provides all the background logic for formatting the meta value., (*15)
Then the main functionality is accessed through the HasMeta trait., (*16)
Here are a few examples (checkout the docs folder for more details information)., (*17)
class User extends Eloquent{
use LeeMason\Metable\HasMeta;
protected $metaModel = 'UserMeta';
}
class UserMeta extends LeeMason\Metable\MetaModel{
// By extending the MetaModel we dont have to set the $fillable or $casts properties!
// Or you can just use the trait
}
$user = new User();
// uses Eloquent firstOrNew to either create or fetch/update the field by "key"
$user->addMeta('key', 'value');
// simple wrapper around addMeta for readability
$user->updateMeta('collection', new Collection(['collection', 'items']));
// need to save lots of data? not a problem
$user->fillMeta([
'meta1' => true,
'meta2' => 200,
'meta3' => [1,2,3],
....
]);
// deleting is easy too
$user->deleteMeta('meta2');
// this will return a Collection object
$collection = $user->getMeta('collection');
// and of course, the meta data are related models so can be accessed, or set as such too
$user->meta();
//or
$user->meta
//and
$meta = new UserMeta();
$meta->key = 'thekey';
$meta->value = 'some value';
$user->meta()->save($meta);