augustpermana / laravel-meta-generator
A Laravel package to generate and manage metadata for models
Installs: 16
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel-package
Requires
- php: >=7.3
- ext-json: *
- illuminate/database: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/events: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
- laravel/framework: >=8.0
README
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.
Table of Contents
Installation
There are two ways to integrate Laravel Meta Generator into your project:
1. Via Packagist
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, ],
2. Using a Local Repository
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.
Usage
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.
Attaching Metadata to a Model
-
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'sModels
directory. This file extends the package’s baseMetaModel
class. -
Updates the Original Model: You must manually update your original model file (e.g.,
Book.php
) to include theHasMetadata
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
).
-
Managing Metadata
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();
Artisan Commands
make:metadata
This command sets up the metadata infrastructure for a specific model.
Usage:
php artisan make:metadata --model=ModelName
What It Does:
- Generates a new meta model file in
app/Models
(e.g.,ModelNameMeta.php
). - Requires you to manually update your original model to include the
HasMetadata
trait. - Creates a migration to build the metadata table (e.g.,
model_name_meta
).
metadata:clean-orphaned
This command cleans up metadata records that no longer have an associated parent model.
Usage:
php artisan metadata:clean-orphaned --model=ModelName
What It Does:
- Scans the metadata table for records with missing parent entries.
- Prompts for confirmation before deleting any orphaned records.
- Provides a summary of the changes performed.
Configuration
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.
License
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.