This will be a very short post explaining a very quick and easy to use solution for adding Meta Fields to an entity(DB table) when you are using the Laravel Framework.
This can be useful in the following scenarios:
- You have an entity that have a lot of attributes, for example a car, it might have some basic properties which you can be sure that every car will have, e.g Engine Capacity, Color, Model, Year of manufacture. And then there will be properties which will vary or be optional for different cars e.g Sunroof, Parking Sensor, Rear view cameras etc. So now you can put the basic properties into the “cars” Table and keep everything else in a “cars_meta” table. And store all this meta information with a very fluid API using Metable.
- You have already created an Entity, which will now have some additional attributes that you didn’t know of or were introduced later on. You can again make use of the same approach as above and instead of making new columns you can add this functionality through Metable.
Download and Usage
Here is the link to the package: https://github.com/kodeine/laravel-meta
The documentation explains the usage pretty well. Just go through the examples mentioned and you will be most likely be able to use it within minutes.
One thing that wasn’t found in the documentation is that Metable doesn’t only support scalar values it also supports compound values like Arrays and Objects.
So you will have another level of flexibility and kind of a No-Sql approach where you can save a multi-property item into a single meta field.
$entity->setMeta('simple', 'This is a simple property value'); // setting a scalar attribute;
$entiry->setMeta('compound', ['type'=>2, 'age'=>4, 'name'=>'something']) // setting a compound attribute
The package will handle the saving and retrieval of compound values.
That’s all :).
Please comment with your real name using good manners.