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

0
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.

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

Discussion

0 comments

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