1. Go to this page and download the library: Download daikazu/laravel-meta 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/ */
daikazu / laravel-meta example snippets
use Daikazu\LaravelMeta\Concerns\HasMeta;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasMeta;
}
use Daikazu\LaravelMeta\Concerns\HasMeta;
use Daikazu\LaravelMeta\Contracts\HasMetadata;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements HasMetadata
{
use HasMeta;
}
$product->meta->get('color'); // mixed, null when absent
$product->meta->get('color', 'red'); // with default
// Typed accessors — each returns null when the key is absent
$product->meta->string('title'); // ?string
$product->meta->integer('views'); // ?int
$product->meta->float('rating'); // ?float
$product->meta->boolean('featured'); // ?bool
$product->meta->array('tags'); // ?array
$product->meta->collection('tags'); // Collection<array-key, mixed>
$product->meta->datetime('published_at'); // ?CarbonImmutable
// All keys at once
$product->meta->all(); // array<string, mixed>
$product->meta->set('color', 'blue');
// Multiple keys in one call (single DB round-trip for non-dot keys)
$product->meta->setMany([
'color' => 'blue',
'featured' => true,
'rating' => 4.5,
]);
// Alias for setMany
$product->meta->upsert(['color' => 'green']);
// Sync — identical to setMany but removes keys not in the payload
$product->meta->sync(['color' => 'green', 'weight' => 1.2]);
// keys not listed ('featured', 'rating' above) are deleted
$product->meta->forget('color');
$product->meta->forgetMany(['color', 'rating']);
$product->meta->flush(); // remove every key for this model
$product = new Product(['name' => 'New']);
$product->meta->set('featured', true);
$product->save(); // buffered meta is written here
class Product extends Model
{
use HasMeta;
protected array $metaCasts = [
'featured' => 'boolean',
'rating' => 'float',
'published_at' => 'datetime',
'tags' => 'array',
'seo' => SeoData::class, // value object (see below)
];
}
use Daikazu\LaravelMeta\Contracts\MetaCast;
use Illuminate\Database\Eloquent\Model;
class MoneyCast implements MetaCast
{
public function get(mixed $value, string $key, Model $model): Money
{
return new Money((int) $value['amount'], (string) $value['currency']);
}
public function set(mixed $value, string $key, Model $model): array
{
return ['amount' => $value->amount, 'currency' => $value->currency];
}
}
use Daikazu\LaravelMeta\Contracts\Castable;
use Daikazu\LaravelMeta\Contracts\MetaCast;
class Money implements Castable
{
public static function castUsing(): MetaCast
{
return new MoneyCast;
}
}
// The value object — no cast interface function __construct(
public string $title,
public ?string $description = null,
) {}
}
// Model
protected array $metaCasts = [
'seo' => SeoData::class,
];
// Usage
$product->meta->set('seo', new SeoData(title: 'My Product', description: 'Great.'));
$seo = $product->meta->get('seo'); // SeoData instance
// Migration snippet
$table->json('meta_data')->nullable();
// Model
class Page extends Model
{
use HasMeta;
protected string $metaColumn = 'meta_data';
protected $casts = ['meta_data' => 'array'];
}
// Equality (shorthand: omit operator for '=')
Product::whereMeta('featured', true)->get();
Product::whereMeta('rating', '>', 4)->get();
// OR variant
Product::whereMeta('featured', true)
->orWhereMeta('featured', false)
->get();
// IN / NOT IN
Product::whereMetaIn('category', ['books', 'games'])->get();
Product::whereMetaNotIn('status', ['archived'])->get();
// NULL / NOT NULL (works with dot-notation keys)
Product::whereMetaNull('seo.description')->get();
Product::whereMetaNotNull('published_at')->get();
// BETWEEN
Product::whereMetaBetween('rating', [3, 5])->get();
// ORDER BY
Product::orderByMeta('rating', 'desc')->get();
$products = Product::withMeta()->get();
// Each $product->meta->get('...') now reads from memory.
// Also available directly when you need the raw relation
$product->metaRecords; // Collection<int, Meta>
// config/meta.php
'cache' => [
'enabled' => true, // set false to disable entirely
'store' => null, // null = default cache store; or e.g. 'redis'
'ttl' => 3600, // seconds
'prefix' => 'meta',
],
public function __construct(
public Model $model,
public string $key,
public mixed $oldValue,
public mixed $newValue,
) {}
use Daikazu\LaravelMeta\Events\MetaUpdated;
Event::listen(MetaUpdated::class, function (MetaUpdated $event): void {
logger("Meta key [{$event->key}] changed on {$event->model->getMorphClass()} #{$event->model->getKey()}");
});
use Daikazu\LaravelMeta\Schema\NumberMeta;
use Daikazu\LaravelMeta\Schema\TextMeta;
use Daikazu\LaravelMeta\Schema\ToggleMeta;
// In AppServiceProvider::boot()
Product::metaSchema([
TextMeta::make('seo.title')->