Skip to content

Ecommerce Report Walkthrough

After this page, you can design and execute a real ecommerce report flow using source definitions, preview, and export.

Scenario

  • 1000 users
  • each user has 10 orders
  • each order amount is 300 taka

Expected totals:

  • total orders = 1000 * 10 = 10,000
  • total revenue = 10,000 * 300 = 3,000,000 taka

What works today

Current package phase supports:

  • registering data sources
  • defining safe columns/fields
  • report definition validation
  • preview execution
  • CSV and XLSX export

Step 1: Design your source fields

Use only trusted fields needed for reporting.

php
new EloquentDataSource(
    key: 'orders',
    label: 'Orders',
    model: Order::class,
    fields: [
        Field::integer('user_id')
            ->label('User')
            ->column('orders.user_id')
            ->groupable()
            ->filterable([FilterOperator::Equals, FilterOperator::In]),
        Field::decimal('total_amount')
            ->label('Order Amount')
            ->column('orders.total_amount')
            ->sortable()
            ->groupable()
            ->filterable([FilterOperator::Equals, FilterOperator::GreaterThan, FilterOperator::Between])
            ->aggregates([AggregateFunction::Sum, AggregateFunction::Avg]),
        Field::dateTime('created_at')
            ->label('Order Date')
            ->column('orders.created_at')
            ->sortable()
            ->filterable([FilterOperator::DateAfter, FilterOperator::DateBefore, FilterOperator::ThisMonth]),
    ],
);

Step 2: Register the data source

php
$reportBuilder->registerDataSource(new OrdersDataSource());

Step 3: Build a report definition

php
<?php

use Ihasan\ReportBuilder\DTOs\FilterCondition;
use Ihasan\ReportBuilder\DTOs\FilterGroup;
use Ihasan\ReportBuilder\DTOs\OutputDefinition;
use Ihasan\ReportBuilder\DTOs\ReportDefinition;
use Ihasan\ReportBuilder\DTOs\SelectedColumn;
use Ihasan\ReportBuilder\DTOs\SortDefinition;
use Ihasan\ReportBuilder\Enums\FilterOperator;

$definition = new ReportDefinition(
    sourceKey: 'orders',
    selectedColumns: [
        new SelectedColumn('user_id', 'User'),
        new SelectedColumn('total_amount', 'Order Amount'),
        new SelectedColumn('created_at', 'Order Date'),
    ],
    filters: new FilterGroup('and', [
        new FilterCondition('total_amount', FilterOperator::Between, [100, 1000]),
        new FilterCondition('created_at', FilterOperator::ThisMonth),
    ]),
    sortDefinitions: [new SortDefinition('total_amount', 'desc')],
    outputDefinition: new OutputDefinition('csv', 'ecommerce-orders.csv'),
);

Step 4: Run preview

php
<?php

use Ihasan\ReportBuilder\Execution\PreviewRunner;

$preview = app(PreviewRunner::class)->preview($definition, perPage: 25, page: 1);

Step 5: Export the report

php
<?php

use Ihasan\ReportBuilder\Execution\ReportRunner;

$export = app(ReportRunner::class)->export($definition);

$export includes filename, mime_type, and content.

Execution flow

Parse error on line 1:
flowchart LR    UI[
^
Expecting 'NEWLINE', 'SPACE', 'GRAPH', got 'ALPHA'

Practical note for your example

For your ecommerce dataset, the desired report is:

  • group by user_id
  • aggregate sum(total_amount) as total spend per user

This same source definition powers preview and export safely through allowlisted fields and operators.