Download the PHP package necenzurat/eloquent-meta without Composer
On this page you can find all versions of the php package necenzurat/eloquent-meta. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package eloquent-meta
Fluent Meta Data for Eloquent Models
A fork from kodeine/laravel-meta updated and compatible with Laravel 5.x (including 5.5)
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
Add this to your composer.json file, in the require object:
After that, run composer install to install the package.
Todo: make an generator for this
Migration Table Schema
Configuration
Model Setup
Next, add the Metable
trait to each of your metable model definition:
Metable Trait will automatically set the meta table based on your model name.
Default meta table name would be, model_meta
.
In case you need to define your own meta table name, you can specify in model:
Gotcha
When you extend a model and still want to use the same meta table you must override getMetaKeyName
function.
Working With Meta
Setting Content Meta
To set a meta value on an existing piece of content or create a new data:
Fluent way, You can set meta flawlessly as you do on your regular eloquent models. Metable checks if attribute belongs to model, if not it will access meta model to append or set a new meta.
Or
Or set multiple metas
at once:
Note: If a piece of content already has a meta the existing value will be updated.
Unsetting Content Meta
Similarly, you may unset meta from an existing piece of content:
Fluent way to unset.
Or
Or unset multiple metas
at once:
Note: The system will not throw an error if the content does not have the requested meta.
Checking for Metas
To see if a piece of content has a meta:
Fluent way, Metable is clever enough to understand $post->content is an attribute of meta.
Retrieving Meta
To retrieve a meta value on a piece of content, use the getMeta
method:
Fluent way, You can access meta data as if it is a property on your model. Just like you do on your regular eloquent models.
Or
Or specify a default value, if not set:
You may also retrieve more than one meta at a time and get an illuminate collection:
Retrieving All Metas
To fetch all metas associated with a piece of content, use the getMeta
without any params
Retrieving an Array of All Metas
To fetch all metas associated with a piece of content and return them as an array, use the toArray
method:
Meta Table Join
When you need to filter your model based on the meta data , you can use meta
scope in Eloquent Query Builder.
Eager Loading
When you need to retrive multiple results from your model, you can eager load metas