Display a relation count in a List via columns.yaml
5
Given you have a model that uses a count
property on a relationship to get only the related model count:
public $hasOne = [
'comments_count' => [Comment::class, 'count' => true],
];
To display this count value in a List, use the useRelationCount
option in your columns.yaml
. Using the usual select: count
method would result in a Unknown column 'count' in 'field list'
error.
comments_count:
label: Number of related comments
type: number
relation: comments_count
useRelationCount: true # this is important
how to cache the result? I have over 100,000 linked records and this example is very slow