PHP code example of kolossal-io / laravel-multiplex
1. Go to this page and download the library: Download kolossal-io/laravel-multiplex 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/ */
kolossal-io / laravel-multiplex example snippets
$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);
// You may also schedule changes, for example change the meta in 2 years:
$post->setMetaAt('likes', 6000, '+2 years');
use Illuminate\Database\Eloquent\Model;
use Kolossal\Multiplex\HasMeta;
class Post extends Model
{
use HasMeta;
}
$model->setMeta('foo', 'bar');
// or
$model->foo = 'bar';
// Save the given meta value right now.
$model->saveMeta('foo', 123.45);
// Save only specific keys of the changed meta.
$model->setMeta(['color' => '#fff', 'hide' => false]);
$model->saveMeta('color');
$model->isMetaDirty('hide'); // true
// Save multiple meta values at once.
$model->saveMeta([
'color' => '#fff',
'hide' => true,
]);
$user = Auth::user();
$user->saveMeta('favorite_band', 'The Mars Volta');
$user->saveMetaAt('favorite_band', 'Portishead', '+1 week');
// Changing taste in music: This will return `The Mars Volta` now but `Portishead` in a week.
$user->favorite_band;
$user->saveMetaAt('favorite_band', 'Arctic Monkeys', '-5 years');
$user->saveMetaAt('favorite_band', 'Tool', '-1 year');
// This will return `Tool` – which is true since this is indeed a good band.
$user->favorite_band;
$post = Post::find(1);
$post->color; // string(4) "#000"
$post->likes; // int(24)
$post->hide; // bool(true)
// In the year 2030 `$post->color` will be `#fff`.
Meta::published()->get(); // Only current and historic meta.
Meta::planned()->get(); // Only meta not yet published.
Meta::publishedBefore('+1 week')->get(); // Only meta published by next week.
Meta::publishedAfter('+1 week')->get(); // Only meta still unpublished in a week.
Meta::onlyCurrent()->get(); // Only current meta without planned or historic data.
Meta::withoutHistory()->get(); // Query without stale records.
Meta::withoutCurrent()->get(); // Query without current records.
// Get records that have been current a month ago.
Meta::onlyCurrent('-1 month')->get();
// Get records that will not be history by tommorow.
Meta::withoutHistory(Carbon::now()->addDay())->get();
// Find posts having at least one meta records for `color` key.
Post::whereHasMeta('color')->get();
// Or pass an array to find records having meta for at least one of the given keys.
Post::whereHasMeta(['color', 'background_color'])->get();
// Find posts not having any meta records for `color` key.
Post::whereDoesntHaveMeta('color')->get();
// Or find records not having meta for any of the given keys.
Post::whereDoesntHaveMeta(['color', 'background_color'])->get();
// Find posts where the current attached color is `black`.
Post::whereMeta('color', 'black')->get();
// Find posts where the current attached color is not `black`.
Post::whereMeta('color', '!=', 'black')->get();
// Find posts that are `visible`.
Post::whereMeta('visible', true)->get();
// There are alternatives for building `or` clauses for all scopes.
Post::whereMeta('visible', true)->orWhere('hidden', false)->get();
// Matches only meta records with type `boolean`.
Post::whereMeta('hidden', false)->get();
// Matches only meta records with type `datetime`.
Post::whereMeta('release_at', '<=', Carbon::now())->get();
// Find posts where `color` is `black` (string) or `false` (boolean).
Post::whereMetaIn('color', ['black', false])->get();
Post::whereMetaEmpty('favorite_band')->get();
// Get all posts having meta names `likes` and `comments` where *both* of them are not empty.
Post::whereMetaNotEmpty(['likes', 'comments'])->get();
use Kolossal\Multiplex\Events\MetaHasBeenAdded;
class SomeListener
{
public function handle(MetaHasBeenAdded $event)
{
$event->meta; // The Meta model that was added.
$event->model; // The parent model, same as $event->meta->metable.
$event->type; // The class name of the parent model.
}
}
use Kolossal\Multiplex\Events\MetaHasBeenRemoved;
class SomeListener
{
public function handle(MetaHasBeenRemoved $event)
{
$event->meta; // The Meta model that was removed.
$event->model; // The parent model, same as $event->meta->metable.
$event->type; // The class name of the parent model.
}
}
$user = Auth::user()->withMetaAt('-1 week');
$user->favorite_band; // Tool
$user->withMetaAt(Carbon::now())->favorite_band; // The Mars Volta
Post::travelTo(Carbon::now()->subYear())->where('category', 'tech')->get();
Post::where('category', 'tech')->get(); // Will still look for meta published last year.
Post::travelBack();
Post::where('category', 'tech')->get(); // Find current meta.
class Post extends Model
{
use HasMeta;
protected array $metaKeys = [
'color',
'hide',
];
}
protected array $metaKeys = ['*'];
$model->metaKeys(['color', 'hide']);
use Kolossal\Multiplex\MetaAttribute;
class Post extends Model
{
use HasMeta;
protected $metaKeys = [];
protected $casts = [
'body' => MetaAttribute::class,
];
}
class Post extends Model
{
use HasMeta;
protected $metaKeys = [
'*',
'body',
];
}
\DB::table('posts')->create(['title' => 'A title', 'body' => 'A body.']);
$post = Post::first();
$post->body; // A body.
$post->body = 'This. Is. Meta.';
$post->save();
$post->body; // This. Is. Meta.
$post->deleteMeta('body');
$post->body; // A body.
// Delete all meta records for the `color` key.
$post->deleteMeta('color');
// Or delete all meta records associated with the model.
$post->purgeMeta();
// Worst case: 26 queries if `color` is a meta value.
$colors = Post::take(25)->get()->map(
fn ($post) => $post->color;
);
// Same result with only 2 queries.
$colors = Post::with('meta')->take(25)->get()->map(
fn ($post) => $post->color;
);
enum SampleEnum: string
{
case Hearts = 'hearts';
case Diamonds = 'diamonds';
}
$model->saveMeta('some_key', SampleEnum::Diamonds);
// true
$model->some_key === SampleEnum::Diamonds;