PHP code example of ibekzod / visual-report-builder

1. Go to this page and download the library: Download ibekzod/visual-report-builder 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/ */

    

ibekzod / visual-report-builder example snippets




use Ibekzod\VisualReportBuilder\Facades\VisualReportBuilder;

// Execute a report configuration
$result = VisualReportBuilder::execute([
    'model' => 'App\Models\Order',
    'row_dimensions' => ['region', 'status'],
    'column_dimensions' => ['month'],
    'metrics' => [
        [
            'column' => 'amount',
            'aggregate' => 'sum',
            'alias' => 'total_sales'
        ],
        [
            'column' => 'id',
            'aggregate' => 'count',
            'alias' => 'order_count'
        ]
    ],
    'filters' => [
        'status' => ['completed', 'paid']
    ]
]);

return response()->json($result);

return [
    // Authentication
    'auth' => [
        'enabled' => env('VISUAL_REPORT_AUTH_ENABLED', false),
        'web_middleware' => env('VISUAL_REPORT_WEB_MIDDLEWARE', ''),
        'api_middleware' => env('VISUAL_REPORT_API_MIDDLEWARE', ''),
    ],

    // Model auto-discovery
    'models' => [
        'auto_discover' => env('VISUAL_REPORT_AUTO_DISCOVER', true),
        'namespace' => env('VISUAL_REPORT_MODEL_NAMESPACE', 'App\\Models'),
        'path' => env('VISUAL_REPORT_MODEL_PATH', null), // defaults to app_path('Models')
    ],

    // Permissions
    'permissions' => [
        'create_templates' => env('VISUAL_REPORT_CREATE_TEMPLATES', 'all'),
        // Options: 'all', 'admin', or specific role name
    ],

    // Export options
    'exporters' => [
        'csv' => true,
        'excel' => true,
        'pdf' => true,
        'json' => true,
    ],

    // Cache settings
    'cache' => [
        'enabled' => env('VISUAL_REPORT_CACHE_ENABLED', true),
        'ttl' => env('VISUAL_REPORT_CACHE_TTL', 3600),
    ],

    // Pivot table limits
    'pivot' => [
        'max_dimensions' => env('VISUAL_REPORT_MAX_DIMENSIONS', 5),
        'max_metrics' => env('VISUAL_REPORT_MAX_METRICS', 10),
        '

// Your existing model - no changes needed
class Order extends Model
{
    protected $fillable = ['amount', 'status', 'region', 'customer_id'];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

// The package automatically:
// - Discovers the Order model
// - Extracts all columns as dimensions or metrics
// - Detects the customer relationship



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ibekzod\VisualReportBuilder\Traits\Reportable;
use Ibekzod\VisualReportBuilder\Traits\HasDimensions;
use Ibekzod\VisualReportBuilder\Traits\HasMetrics;

class Order extends Model
{
    use Reportable, HasDimensions, HasMetrics;

    public static function dimensions(): array
    {
        return [
            ['column' => 'region', 'label' => 'Sales Region', 'type' => 'string'],
            ['column' => 'status', 'label' => 'Order Status', 'type' => 'string'],
            ['column' => 'created_at', 'label' => 'Order Date', 'type' => 'date'],
        ];
    }

    public static function metrics(): array
    {
        return [
            [
                'column' => 'amount',
                'label' => 'Order Amount',
                'type' => 'decimal',
                'default_aggregate' => 'sum'
            ],
            [
                'column' => 'quantity',
                'label' => 'Quantity',
                'type' => 'integer',
                'default_aggregate' => 'sum'
            ]
        ];
    }
}

// Order model
class Order extends Model
{
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

// In the builder:
// 1. Select Order as data source
// 2. Choose "customer" from the relationship dropdown
// 3. Customer fields appear with prefix: "customer.region", "customer.name"
// 4. Drag "customer.region" to dimensions
// 5. The query automatically JOINs the customers table
bash
php artisan migrate
bash
php artisan vendor:publish --tag=visual-report-builder-config
bash
php artisan route:clear
php artisan cache:clear
php artisan config:clear