Relationships
A foreign key column holds an internal id, which never reaches the browser. A TextColumn shows the title of the referenced record instead, optionally linked to its detail page.
TextColumn::make('customer_id')->label('Customer')->relationship(CustomerModel::class, 'name')->linkToRecord(),TextColumn::make('cost_center_id')->label('Cost center')->relationship(CostCenterModel::class, 'number'),TextColumn::make('budget_owner_id')->label('Budget owner')->user(),Referenced records
Section titled “Referenced records”relationship(Model::class, 'title') looks up the record with the id of
the column and shows the given attribute (default name).
Which titles are shown follows the same rules as the BelongsTo field:
- With a resource for the model in the panel: only records the user may
see there (tenant,
scopeQuery(), view permission, policies). Others are shown empty – a title never leaks through a list. - Without a resource: the tenant scoped records of the model. A model
without tenant scope is refused in multi-tenant installations
(
LogicException).
The related records are loaded once per request and reused for all rows,
which suits small and medium tables (customers, cost centers,
departments). For large related tables, join the title in scopeQuery()
and show it with queryColumn(), see
Columns of a join –
that is also the way to sort and search by the title.
Links to the record
Section titled “Links to the record”linkToRecord() links the title to the detail page of the referenced
record:
TextColumn::make('project_id')->label('Project')->relationship(ProjectModel::class, 'name')->linkToRecord(),TextColumn::make('project_id')->relationship(ProjectModel::class, 'name')->linkToRecord(target: Link::NEW),The link is only rendered when the related model has a resource in the
current panel and the user may view the record; otherwise the title is
plain text. target is Link::SAME (default for internal links),
Link::NEW or a tab name.
user() shows the username for a column with the internal id of a user,
e.g. the owner or the budget owner:
TextColumn::make('created_by')->label('Created by')->user(),Like the UserSelect field it needs no access to the user administration and shows usernames only (no e-mail addresses), of members of the current tenant. Deactivated users are shown as well, so old records keep their owner.
Filters
Section titled “Filters”The matching filter is a SelectFilter with relationship(), see
SelectFilter.