• Reading time ~ 1 min
  • 04.12.2022

Laravel Multiplex is a Laravel package to attach time-sliced metadata to Eloquent models. With the v1.0 release near, here are the main features:

  • Metadata is saved in versions, including the ability to schedule metadata for the future
  • Fluent syntax
  • East to try extending your model with versionable metadata without touching original columns
  • Type conversion system
  • Configurable

Here are a few basic examples of how this package looks working with a model:

$post = \App\Models\Post::first();
 
// Set meta fluently for any key – `likes` is no column of `Post`.
$post->likes = 24;
 
// Or use the `setMeta` method.
$post->setMeta('likes', 24);
 
// Set multiple values
$model->setMeta([
    'hide' => true,
    'color' => '#000',
    'likes' => 24,
]);
 
// You may also schedule changes, for example, change the meta in 2 years:
$post->setMetaAt('likes', 6000, '+2 years');

You can also limit which meta keys are allowed on a model with a $metaKeys property:

class Post extends Model
{
    use HasMeta;
 
    protected array $metaKeys = [
        'color',
        'hide',
    ];
 
    // You can use typecast array keys
    protected array $metaKeys = [
        'foo',
        'count' => 'integer',
        'color' => 'string',
        'hide' => 'boolean',
    ];
 
}

When you set up this package, read the performance section to avoid N+1 queries. To learn more about this package and get full installation instructions, check out the laravel-metadata on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297