Download the PHP package faizansf/laravel-metafields without Composer
On this page you can find all versions of the php package faizansf/laravel-metafields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download faizansf/laravel-metafields
More information about faizansf/laravel-metafields
Files in faizansf/laravel-metafields
Package laravel-metafields
Short Description This package enhances Laravel Models by introducing Meta field functionality.
License MIT
Homepage https://github.com/faizansf/laravel-metafields
Informations about the package laravel-metafields
Laravel Metafields
The Laravel Metafields package is a versatile and powerful tool designed for Laravel developers who need to extend their models with metafield functionality. This package enables you to effortlessly attach additional custom fields (metafields) to any Eloquent model in your Laravel application, providing a seamless way to enhance your models with extra data without altering your database schema.
Use Cases:
This package is ideal for projects that require additional data storage like CMS, e-commerce platforms, and custom CRM systems. It's particularly useful in scenarios where the database schema needs to remain unchanged while still allowing for data extension.
Installation
You can install the package via composer:
You can publish and run the migrations with:
Note:
Before running the migrations make sure you have set the correct configuration
You can publish the config file with:
Configuration
Integrating in Model
Integrate the Metafiedable
contract and the HasMetafields
trait into your model
Usage
To set a metafield, use string or string backed enum key and value as below:
To get metafield value use:
You can also provide a default value when getting a metafield
Note:
A default value is not persisted in the database and is just returned whenever the actual value is null
Similarly, metafields can be deleted as follows:
Cache
Caching is enabled by default, but can be disabled in your metafields.php
configuration file. To control caching
behavior in your model class, add the $shouldCacheMetafields
property. Setting this property in your model will
override the default caching configuration. Additionally, you can specify a custom time-to-live (TTL) for the
cache by adding the $ttl
property to your model, allowing for fine-tuned cache duration control.
In the metafields.php
config file
Or in your model class
You can retrieve a non-cached version of the data by using the withoutCache()
method. This method provides a
straightforward way to bypass caching for a single call, ensuring you get the most up-to-date data.
Serialization
The package includes StandardValueSerializer
, DirectValueSerializer
and JsonValueSerializer
classes.
You can choose a default serializer for all fields in the metafields.php
configuration file.
Additionally, you can define
a $metafieldSerializers
array inside your model, or you can implement a protected registerSerializers()
method
in your model to override the default serialization behavior.
The registerSerializers()
method will then use mapSerializer()
method provided by HasMetafields
trait
to register the serializers.
Any custom serializer class you add must
implement the FaizanSf\LaravelMetafields\Contracts\ValueSerializer
interface.
And then in your model
Alternatively, you can also define $metafieldSerializers
property directly into your model
Note:
Due to PHP's restriction where enums can't be used as array keys, we need to utilize the enum values for mapping serializers.
In situations where you already possess a string value that doesn't require serialization, the DirectValueSerializer
can be used.
This allows you to bypass the usual serialization process, streamlining the handling of such
pre-formatted or non-serializable values.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Faizan Shakil Faruqi
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-metafields with dependencies
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^10.0