PHP code example of faizansf / laravel-metafields

1. Go to this page and download the library: Download faizansf/laravel-metafields library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

faizansf / laravel-metafields example snippets


return [
    // The name of the database table to store metafields.
    'table' => 'metafields',

    // The name of the column in the 'meta_fields' table that references the model.
    'model_column_name' => 'model',

    // An array of classes that are allowed to be unserialized.
    'unserialize_allowed_class' => [],

    // The class responsible for serializing the values stored in metafields.
    'default_serializer' => \FaizanSf\LaravelMetafields\Support\ValueSerializers\StandardValueSerializer::class,

    // Flag to enable or disable caching of metafields.
    'cache_metafields' => true,

    // Time-to-live for cached meta fields. Null indicates caching forever.
    'cache_ttl' => null,

    // The prefix used for cache keys when storing individual metafield values.
    'cache_key_prefix' => 'LaravelMetafields',

    //Block keys from being used as metafield keys
    'not_allowed_keys' => [],
    
    //Cache key for all metafields collection
    'all_metafields_cache_key' => 'all-metafields'
];

...
use FaizanSf\LaravelMetafields\Concerns\HasMetafields;
use FaizanSf\LaravelMetafields\Contracts\Metafieldable;

class Person extends Model implements Metafieldable
{
    use HasMetafields;
     
    ...
}

$person = Person::find(1);

//using HasMetafields trait
$person->setMetafield('some-key', 'value')

//using HasMetafields trait
$person->getMetafield('some-key');
$person->getAllMetafields();

//using HasMetafields trait
$person->getMetafield('some-key', 'default value');

//using HasMetafields trait
$person->deleteMetafield('some-key');
$person->deleteAllMetaField('some-key');

[
    ...
    // Flag to enable or disable caching of metafields.
    'cache_metafields' => true,
    ...
]

...
class Person extends Model implements Metafieldable
{
    use HasMetafields;
    
    protected $shouldCacheMetafields = true;
    protected $ttl = 600
}

//using HasMetafields trait
$person->withoutCache()->getMetafield('some-key');
$person->withoutCache()->getAllMetafields();

namespace App\ValueSerializers;

use FaizanSf\LaravelMetafields\Contracts\ValueSerializer;
use Illuminate\Support\Arr;

class CustomSerializer implements ValueSerializer
{
    public function unserialize($serialized): mixed
    {
        //Do some custom logic here
    }

    public function serialize($value): string
    {
        //Do some custom logic here
    }
}

...
class Person extends Model implements Metafieldable
{
    use HasMetafields;
    
    protected function registerSerializers(){
        $this
            ->mapSerializer('some-key', CustomSerializer::class)
            ->mapSerializer(PersonMetafieldsEnum::Name, CustomSerializer::class)
    }
}

...
class Person extends Model implements Metafieldable
{
    use HasMetafields;
    
    protected array $metafieldSerializers  = [
         'my-custom-metafield' => CustomSerializer::class,
         ExampleMetafieldsEnum::ExampleField->value => CustomSerializer::class
    ];
}
bash
php artisan vendor:publish --tag="metafields-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="metafields-config"