augustpermana/laravel-meta-generator
Attach and manage key-value metadata for Eloquent models without altering main tables. Provides auto type detection and casting, a MetaModel base, and artisan commands to generate meta models and clean orphaned records for easy setup and maintenance.
Laravel Meta Generator is a powerful package that enables you to easily attach and manage metadata for your Eloquent models without modifying their primary database tables. It provides a flexible key-value system featuring automatic type detection, casting, and handy artisan commands to simplify installation and maintenance.
There are two ways to integrate Laravel Meta Generator into your project:
Run the following command:
composer require augustpermana/laravel-meta-generator
Laravel will automatically discover the service provider via package discovery. If necessary, manually add the provider in your config/app.php:
'providers' => [
// Other Service Providers
August\MetaGenerator\ServiceProvider::class,
],
If the package isn’t published on Packagist yet, add it as a local repository. Modify your project's composer.json:
"repositories": [
{
"type": "path",
"url": "../laravel-meta-table"
}
],
Then run:
composer require augustpermana/laravel-meta-generator
This configuration instructs Composer to use the specified local path for the package.
Laravel Meta Generator lets you attach metadata to your models without modifying the original database tables. Once installed, you can either use the provided artisan commands or manually integrate the functionality into your models.
Generate Metadata Files:
Run the artisan command to set up the metadata system for an existing model. For example, for a Book model:
php artisan make:metadata --model=Book
When you run this command, it performs the following actions:
Creates a Meta Model File: Generates a new file (e.g., BookMeta.php) in your application's Models directory. This file extends the package’s base MetaModel class.
Updates the Original Model: You must manually update your original model file (e.g., Book.php) to include the HasMetadata trait. For example:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use AugustPermana\MetaGenerator\Traits\HasMetadata;
class Book extends Model
{
use HasMetadata;
}
Creates a Migration: Generates a migration to create the corresponding metadata table (e.g., book_meta).
Once set up, your model gains several useful methods via the HasMetadata trait:
Retrieving Metadata:
$value = $book->getMeta('author');
Setting Metadata:
$book->setMeta('publisher', 'Acme Publishing');
Bulk Updating Metadata:
$book->setManyMeta([
'isbn' => '1234567890',
'pages' => 350,
'published_at' => '2025-02-22'
]);
Syncing Metadata:
$book->syncMeta([
'genre' => 'Fiction',
'language' => 'English'
]);
Querying Models by Metadata:
// Retrieve models with any value for the "author" metadata
$models = Model::whereHasMeta('author')->get();
// Retrieve models with a specific "author" value
$models = Model::whereHasMeta('author', 'John Doe')->get();
make:metadataThis command sets up the metadata infrastructure for a specific model.
Usage:
php artisan make:metadata --model=ModelName
What It Does:
app/Models (e.g., ModelNameMeta.php).HasMetadata trait.model_name_meta).metadata:clean-orphanedThis command cleans up metadata records that no longer have an associated parent model.
Usage:
php artisan metadata:clean-orphaned --model=ModelName
What It Does:
No additional configuration is required. Simply run the artisan commands as needed. Ensure that your models are located in the default directory (e.g., app/Models) or adjust the paths accordingly if customized.
Laravel Meta Generator is open-sourced software licensed under the MIT license.
This documentation provides an overview of the package's functionality and usage. For more details and further customization options, please refer to the source code.
How can I help you explore Laravel packages today?