Download the PHP package kodeine/laravel-meta without Composer
On this page you can find all versions of the php package kodeine/laravel-meta. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-meta
Fluent Meta Data for Eloquent Models
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.
Changelog
visit CHANGELOG.md
Installation
Composer
Laravel can be installed on laravel 8.x
or higher.
Run:
For laravel 7.x or below visit this link.
Upgrade guide
Change this line in composer.json
:
to:
after that, run composer update
to upgrade the package.
Upgrade notice
Laravel meta 2 has some backward incompatible changes that listed below:
- Laravel 7 or lower not supported.
-
Removed the following methods:
__get
,__set
,__isset
. If you have defined any of these methods, then you probably have something like this in your model:You need to remove
as
operator of the methods. - Removed legacy getter. in older version if you had a method called
getSomething()
then you could access return value of this method using$model->something
. this is no longer the case, and you have to call$model->getSomething()
. - Added new method
setAttribute
that overrides parent method. - Renamed
getMetaDefaultValue
method togetDefaultMetaValue
. - Second parameter of
getMeta
method is now default value when meta is null. - Removed
whereMeta
method in favor ofscopeWhereMeta
. example:User::whereMeta($key,$value)->get();
- Removed
getModelKey
method.
Migration Table Schema
Each model needs its own meta table.
This is an example migration. you need change parts of it.
In this example we assume you have a model named Post
.
Meta table name should be your model's table name + _meta
which in this case, model's table name is pluralized form of the model name. so the table name becomes posts_meta
.
If you don't want to follow this naming convention and use something else for table name, make sure you add this name to your model's body:
the foreign key name should be your model's name + _id
= post_id
If you used something else for foreign key, make sure you add this to your model's body:
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, models_meta
where models
is pluralized form of the model name.
In case you need to define your own meta table name, you can specify in model:
Default Model Attribute values
Additionally, you can set default values by setting an array called $defaultMetaValues
on the model. Setting default has two side effects:
- If a meta attribute does not exist, the default value will be returned instead of
null
. - if you attempt to set a meta attribute to the default value, the row in the meta table will be removed, which will cause the default value to be returned, as per rule 1.
This is being the desired and expected functionality for most projects, but be aware that you may need to reimplement default functionality with your own custom accessors and mutators if this functionality does not fit your needs.
This functionality is most suited for meta entries that note exceptions to rules. For example: employees sick out of office (default value: in office), nodes taken down for maintenance (default value: node up), etc. This means the table doesn't need to store data on every entry which is in the expected state, only those rows in the exceptional state, and allows the rows to have a default state upon creation without needing to add code to write it.
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:
Or set multiple metas and columns
at once:
Note: If a piece of content already has a meta the existing value will be updated.
You can also save metas with saveMeta
without saving the model itself:
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.
You may also check if model has multiple metas:
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:
Note: default values set in defaultMetaValues property take precedence over default value passed to this method.
You may also retrieve more than one meta at a time and get an illuminate collection:
Disable Fluent Access
If you don't want to access metas in fluent way, you can disable it by adding following property to your model:
By setting that property, this package will no longer handle metas in the following ways:
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 retrieve multiple results from your model, you can eager load metas
Prevent metas attribute from being populated
When you convert a model to an array (or json) and you don't need all meta fields, you can create a model's property to prevent metas from being added to the resulting array. You can also use it on eloquent relations.
Events
Laravel meta dispatches several events, allowing you to hook into the following events: metaCreating
, metaCreated
, metaSaving
, metaSaved
, metaUpdating
, metaUpdated
, metaDeleting
and metaDeleted
. Listeners should expect two parameters, first an instance of the model and second, name of the meta that event occurred for it. To enable events you need to add HasMetaEvents
trait to your model:
After that, you can listen for events the same way you do for models.
If you
return false;
in listener of any event ending withing
, that operation will be aborted.
Additional events
There are some additional events that extend existing laravel events. These events don't need HasMetaEvents
trait and like default laravel events, the event listeners should expect one parameter.
Event names: createdWithMetas
, updatedWithMetas
, savedWithMetas
. These events fire exactly like default laravel events except that they are only fired after all metas saved to database.
For example, you may need to access metas inside a queue job after model created. But because metas have not been saved to database yet (metas will be saved to database in saved
event and this event has not been fired yet), job can't access them. by using createdWithMetas
event instead of created
event, the problem will be solved.
There are 3 ways to listen for events:
1. By Defining $dispatchesEvents
Property
2. Using Closures
3. Observers
All versions of laravel-meta with dependencies
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
illuminate/database Version ^8.0|^9.0|^10.0|^11.0
illuminate/events Version ^8.0|^9.0|^10.0|^11.0
ext-json Version *