Skip to content

Register a Source Safely

After this page, you can register a source that exposes only safe, intentional fields.

Problem

You need one source available to the report builder without exposing internal query details.

Prerequisites

  • Package installed
  • A model like Order

Steps

  1. Create an EloquentDataSource.
  2. Add explicit Field definitions.
  3. Register it in app boot.
php
$reportBuilder->registerDataSource(
    new EloquentDataSource(
        key: 'orders',
        label: 'Orders',
        model: Order::class,
        fields: [
            Field::string('status')
                ->column('orders.status')
                ->filterable([FilterOperator::Eq]),
            Field::decimal('total_amount')
                ->column('orders.total_amount')
                ->sortable()
                ->filterable([FilterOperator::Eq, FilterOperator::Between])
                ->aggregates([AggregateFunction::Sum]),
        ],
    )
);

Verify

bash
curl http://your-app.test/report-builder/sources/orders

Confirm only these fields appear.

Common mistakes

  • Registering sources in the wrong lifecycle location.
  • Missing column() when key differs from DB column intent.
  • Overexposing fields that should stay private.