PHP code example of aurorawebsoftware / flexyfield

1. Go to this page and download the library: Download aurorawebsoftware/flexyfield 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/ */

    

aurorawebsoftware / flexyfield example snippets


// Give your model the power
class Product extends Model implements FlexyModelContract
{
    use Flexy;
}

// Create a schema (think of it as a "field template")
Product::createSchema('electronics', 'Electronics');
Product::addFieldToSchema('electronics', 'voltage', FlexyFieldType::STRING);
Product::addFieldToSchema('electronics', 'warranty_years', FlexyFieldType::INTEGER);

// Use it like it was always there
$tv = Product::create(['name' => 'Smart TV']);
$tv->assignToSchema('electronics');
$tv->flexy->voltage = '220V';
$tv->flexy->warranty_years = 2;
$tv->save();

// Query like a boss
Product::where('flexy_voltage', '220V')->get();

// Shoes have sizes and colors
Product::createSchema('footwear', 'Footwear');
Product::addFieldToSchema('footwear', 'shoe_size', FlexyFieldType::INTEGER,
    validationRules: 'roduct::createSchema('books', 'Books');
Product::addFieldToSchema('books', 'isbn', FlexyFieldType::STRING,
    validationRules: 'kers->flexy->color = 'black';

$novel = Product::create(['name' => 'Clean Code']);
$novel->assignToSchema('books');
$novel->flexy->isbn = '9780132350884';
$novel->flexy->author = 'Robert C. Martin';

// B2B customers need company info
Contact::createSchema('b2b', 'Business Customers');
Contact::addFieldToSchema('b2b', 'company_name', FlexyFieldType::STRING);
Contact::addFieldToSchema('b2b', 'employee_count', FlexyFieldType::INTEGER);
Contact::addFieldToSchema('b2b', 'annual_revenue', FlexyFieldType::DECIMAL);

// B2C customers need personal preferences
Contact::createSchema('b2c', 'Individual Customers');
Contact::addFieldToSchema('b2c', 'birthday', FlexyFieldType::DATE);
Contact::addFieldToSchema('b2c', 'interests', FlexyFieldType::JSON,
    fieldMetadata: ['options' => ['tech', 'sports', 'music', 'travel'], 'multiple' => true]);

// Query by segment
$bigCompanies = Contact::whereSchema('b2b')
    ->where('flexy_annual_revenue', '>', 1000000)
    ->get();

// Each tenant can have custom fields!
$tenantSchema = "tenant_{$tenant->id}_leads";

Lead::createSchema($tenantSchema, "{$tenant->name} Leads");
Lead::addFieldToSchema($tenantSchema, 'source', FlexyFieldType::STRING);
Lead::addFieldToSchema($tenantSchema, 'score', FlexyFieldType::INTEGER);

// Tenant-specific fields added via admin panel
foreach ($tenant->customFields as $field) {
    Lead::addFieldToSchema($tenantSchema, $field->name, $field->type);
}

// Single choice
Product::addFieldToSchema('schema', 'size', FlexyFieldType::STRING,
    fieldMetadata: ['options' => ['S', 'M', 'L', 'XL']]);

// Multiple choices (use JSON type!)
Product::addFieldToSchema('schema', 'features', FlexyFieldType::JSON,
    fieldMetadata: ['options' => ['wifi', 'bluetooth', '5g'], 'multiple' => true]);

$phone->flexy->size = 'M';
$phone->flexy->features = ['wifi', '5g'];  // Array!

Product::addFieldToSchema('schema', 'manual', FlexyFieldType::FILE,
    validationRules: 'manual = $request->file('manual');
$product->save();

// Get URLs
$url = $product->getFlexyFileUrl('manual');
$signedUrl = $product->getFlexyFileUrlSigned('manual', now()->addHour()->timestamp);

Product::addFieldToSchema('schema', 'email', FlexyFieldType::STRING,
    validationRules: '>email = 'not-an-email';
$product->save();  // 💥 ValidationException!

Product::addFieldToSchema('schema', 'battery', FlexyFieldType::INTEGER,
    label: 'Battery Capacity',
    fieldMetadata: [
        'group' => 'Technical Specs',
        'placeholder' => 'mAh',
        'hint' => 'Typical range: 3000-5000'
    ]);

// Perfect for building dynamic forms!
$schema->getFieldsGrouped();  // ['Technical Specs' => [...], 'Ungrouped' => [...]]

// Simple
Product::where('flexy_color', 'blue')->get();

// Dynamic method (Laravel magic!)
Product::whereFlexyColor('blue')->get();

// By schema
Product::whereSchema('electronics')->get();
Product::whereSchemaIn(['electronics', 'furniture'])->get();

// Go wild
Product::whereSchema('electronics')
    ->where('flexy_price', '<', 100)
    ->where('flexy_active', true)
    ->orderBy('flexy_price')
    ->get();

// config/flexyfield.php
return [
    'file_storage' => [
        'default_disk' => env('FLEXYFIELD_DEFAULT_DISK', 'public'),
        'default_path' => env('FLEXYFIELD_DEFAULT_PATH', 'flexyfield'),
        'path_structure' => '{model_type}/{schema_code}/{field_name}/{year}/{month}',
        'cleanup_on_delete' => true,      // Auto-delete files when model deleted
        'enable_security_logging' => true, // Log security events
    ],
];

// Setting values before assigning schema
$product->flexy->field = 'x';  // 💥 Exception!

// Always assign first!
$product->assignToSchema('electronics');
$product->flexy->field = 'x';  // ✅

// Wrong query syntax
Product::where('flexy->field', 'x');  // 🚫 Nope

// Use underscore prefix
Product::where('flexy_field', 'x');  // ✅ Yes!
bash
composer migrate
bash
php artisan vendor:publish --tag="flexyfield-config"

resources/boost/guidelines/core.blade.php