• 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 experience in website development and desktop development. I have experience with various tools and technologies such as Bootstrap, Tailwind, HTML5, CSS3, PUG, JS, AlpineJs, jQuery, PHP, MODX, Laravel, NodeJS, Git, Docker, hQuery, Guzzle, Livewire, Blade, Twig, Fenom, and Smarty. I have experience in website development using Symfony, MODX and Laravel. Experie...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297