Download the PHP package touhidurabir/laravel-meta-fields without Composer
On this page you can find all versions of the php package touhidurabir/laravel-meta-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download touhidurabir/laravel-meta-fields
More information about touhidurabir/laravel-meta-fields
Files in touhidurabir/laravel-meta-fields
Package laravel-meta-fields
Short Description A laravel package to handle model specific additional meta fields in an elegant way.
License MIT
Informations about the package laravel-meta-fields
Laravel Meta Fields
A php package for laravel framework to handle model meta data in a elegant way.
Installation
Require the package using composer:
To publish the config and migration file:
Configuration
The meta-field config file contains the configuration option of meta table name and meta model. It also provides an cast option that will be applied to meta table's metas column which contains the meta fields as array, object or collection(Laravel Collection) . Read the config file to know more details .
Usage
To use with a model , just use the HasMeta tarit.
And then use it in such way as
As this package use the Illuminate\Database\Eloquent\Relations\MorphMany to maintain a one to many poly morphic relation between the parent model and metas , it is possible to restrict if a model can have multiple metas or not by overriding the HasMeta traits canHaveMultipleMetas method my returning false as :
Also the metas are state aware , that is if the associated parent model got deleted, force deleted or restored , meta recored will follow the same model record state . This is by default enabled . To disable this override syncWithParent directly in the model calss to return false .
As metas are managed as Polymorphic Relations and use laravel's MorphMany relations, all models related functionality are availabel . But this package also provide some additional methods such .
To Update, the updateMeta method such as :
NOTE : if model can have multiple meta records, calling updateMeta will throw exception as not possible to know which one of multiple meta records to update .
To delete, the deleteMeta methods can be used as :
By default metas will be deleted as soft deleted record as this package uses softDelets trait on meta model . to force delete, pass true as the only argument to deleteMeta method .
This package Provide some handly out of box methods to handle metas of a model instance . such as
To Determine if model instance has any meta associated
which will return boolean based on any meta associated with or not.
To Determine if aspecific meta fields associated
To deletermine if a specific metafields associated with model instance, follow as :
By default it will return boolean to indicate if there is any associated meta field associate with this one . But if need those records also , pass true as the second argument which will return collection of matched metas or empty collection if none found.
To get the meta value
To get the meta value of a a given field for a model instance, follow as :
By default, it will only pass one single and first match value even if there is multiple of of meta records that contains the give meta fields . to get all pass true as the second argument which will return an array as list of all values .
Storing Metas and Validation
To make the storing and validattion process of meta easier , this package provide another trait named WithMetaFields whcih can be used with the FormRequest class . This trait has as an abstract method metaRules that must be defined which will contains the validation rules for metas . The package will also use the field name defined in this methods to determine the meta fields .
Use the trait in the FormRequest class as such :
And then from the controller class,
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
MIT
All versions of laravel-meta-fields with dependencies
illuminate/support Version ^8.61
illuminate/contracts Version ^8.61
illuminate/events Version ^8.61
touhidurabir/laravel-model-uuid Version ^1.0