# Data sources

> Choose an Eloquent query, collection, or custom source and define row identity.

Source: https://docs.inertiax.dev/table/guide/data-sources/

Pass an Eloquent builder to keep filtering, search, sorting, and pagination in the database:

```php
UsersTable::make('users')->data(
    User::query()->where('account_id', $account->id),
);
```

Scope the query to records the current user may view. See [Authorization & security](https://docs.inertiax.dev/table/guide/security/).

## A parent’s related rows

```php
OrdersTable::make('customer-orders')->data($customer->orders());
```

Pass a standard Eloquent `HasMany` relation to show only that parent’s related rows. Its query constraints, model scopes, and selected fields are preserved; table operations leave the supplied relation unchanged. You can also return the relation from a protected `data()` method or deferred closure.

Other relation types and custom `HasMany` subclasses are unsupported. See [Source types](https://docs.inertiax.dev/table/reference/table/#source-types) for the exact boundary and failure behavior.

## Define data in the class

Add a protected `data()` method when the query belongs with the table definition:

```php
protected function data(): mixed
{
    return User::query()->whereKey(auth()->id());
}
```

A fluent `->data($query)` call overrides this method’s result.

To resolve a source later, pass a closure:

```php
UsersTable::make('users')->data(
    fn () => User::query()->whereKey(auth()->id()),
);
```

## Other sources

`data()` also accepts arrays, Laravel collections, Eloquent collections, and model class names. Use [Collections & arrays](https://docs.inertiax.dev/table/guide/collections/) for a complete in-memory example.

```php
UsersTable::make('users')->data(User::class);
```

Caution

A model class becomes `User::query()`. It adds no user or tenant scope beyond the model’s global scopes. Use a scoped builder when access depends on the request. See [Query scoping](https://docs.inertiax.dev/table/guide/security/#authorize-and-scope-the-query).

For an API, search service, or repository that owns retrieval, pass a `TableSource` through the same `data()` method. See [Custom sources](https://docs.inertiax.dev/table/guide/custom-sources/) for a working adapter.

## Row identity

Every row needs a stable, unique key. Eloquent queries use the model’s primary key; ordinary arrays and collections use `id`. Choose another field with `rowKey()`:

```php
use InertiaX\Table\Columns\TextColumn;
use InertiaX\Table\Table;

Table::make('sessions')
    ->data($sessions)
    ->rowKey('uuid')
    ->addColumns([
        TextColumn::make('uuid')->visible(false),
        TextColumn::make('ip_address', 'IP address'),
    ]);
```

The key must be a declared column, including for empty results. Use a persistent identifier, not a position that changes when rows are sorted or filtered.

See [Table API](https://docs.inertiax.dev/table/reference/table/#row-identity) for model collections, integer limits, and validation.

## Related fields and aggregates

Use [`relationship()`](https://docs.inertiax.dev/table/reference/column/#related-scalar-fields) for a persisted scalar field through a standard, single-hop `BelongsTo` relation. Use [`count()` and `sum()`](https://docs.inertiax.dev/table/reference/column/#relationship-counts-and-sums) for standard, single-hop `HasMany` aggregates. These helpers bind the displayed value and built-in comparisons to the same query field. Enable sorting and filtering separately; related text fields can also enable search.

For other relation types or query shapes, use ordinary Eloquent capabilities such as correlated subqueries, `withCount()` or `withSum()`. Declare the resulting field as a column and use `sortUsing()` or `searchUsing()` for its query behavior. A displayed alias alone does not define relationship search or sorting.

A `customer_name` subselect can display a related name while a search callback uses `whereHas('customer', ...)`. See [Custom query behavior](https://docs.inertiax.dev/table/guide/query-callbacks/) for source strategies and [Callback context](https://docs.inertiax.dev/table/reference/callbacks/) for their arguments. For an external service that owns filtering and pagination, use a [custom source](https://docs.inertiax.dev/table/guide/custom-sources/) instead.
