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/ */
// 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