October CMS resources and help articles

Simple and to the point. Optimized by the community.

Display a relation count in a List via columns.yaml

by OFFLINE, last modified on July 30th, 2021

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.

        label: Number of related comments
        type: number
        relation: comments_count
        useRelationCount: true             # this is important


1 comment

Post on December 12th, 2021 9:13 AM

how to cache the result? I have over 100,000 linked records and this example is very slow

We use cookies to measure the performance of this website. Do you want to accept these cookies?