PHP code example of augustpermana / hypervel-meta-generator

1. Go to this page and download the library: Download augustpermana/hypervel-meta-generator 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/ */

    

augustpermana / hypervel-meta-generator example snippets


   

   namespace App\Models;

   use App\Models\Model;
   use AugustPermana\HypervelMetaGenerator\Traits\HasMetadata;

   class Product extends Model
   {
       use HasMetadata;
       
       // ... model code lainnya
   }
   

$product = Product::find(1);
$product->setMeta('warranty_period', 24); // Auto-detected as integer
$product->setMeta('is_featured', true);   // Auto-detected as boolean
$product->setMeta('specifications', [     // Auto-detected as json
    'color' => 'black',
    'weight' => '1.5kg'
]);

$warrantyPeriod = $product->getMeta('warranty_period'); // Returns: 24 (as integer)
$isFeatured = $product->getMeta('is_featured');         // Returns: true (as boolean)
$specs = $product->getMeta('specifications');           // Returns: array

// Dengan default value
$discount = $product->getMeta('discount', 0); // Returns: 0 jika tidak ada

$product->setManyMeta([
    'brand' => 'Samsung',
    'warranty_period' => 24,
    'is_featured' => true,
    'release_date' => '2025-10-22'
]);

// Hapus semua metadata yang ada dan ganti dengan yang baru
$product->syncMeta([
    'brand' => 'Apple',
    'model' => 'iPhone 15',
    'price' => 999.99
]);

if ($product->hasMeta('warranty_period')) {
    // Metadata exists
}

$product->removeMeta('old_field');

// Cari semua produk yang featured
$featuredProducts = Product::whereHasMeta('is_featured', '1')->get();

// Cari semua produk yang punya metadata 'warranty_period'
$productsWithWarranty = Product::whereHasMeta('warranty_period')->get();

protected function getMetaModelClass()
{
    return 'App\\CustomNamespace\\' . class_basename($this) . 'Meta';
}

// ❌ N+1 Problem
$products = Product::all();
foreach ($products as $product) {
    $brand = $product->getMeta('brand'); // Query di setiap loop
}

// ✅ Eager Loading
$products = Product::with('meta')->get();
foreach ($products as $product) {
    $brand = $product->getMeta('brand'); // Tidak ada query tambahan
}

// ❌ Multiple Queries
$product->setMeta('field1', 'value1');
$product->setMeta('field2', 'value2');
$product->setMeta('field3', 'value3');

// ✅ Single Batch
$product->setManyMeta([
    'field1' => 'value1',
    'field2' => 'value2',
    'field3' => 'value3',
]);
bash
   php artisan make:metadata --model=Product
   
bash
   php artisan migrate
   
bash
php artisan make:metadata --model=Product
bash
php artisan metadata:clean-orphaned --model=Product